“Cannot use import statement outside a module” appears in Node.js when you use ES module import syntax in a file that Node.js treats as CommonJS. Node.js defaults to CommonJS (require/module.exports) unless explicitly configured for ES modules. The fix is either configuring your project for ES modules or switching to CommonJS syntax.
This error became extremely common after JavaScript frameworks and tutorials shifted to ES module syntax while Node.js maintained CommonJS as its default. The mismatch between frontend code (always ES modules) and backend code (CommonJS by default) causes this error when copying code between environments or following browser-focused tutorials for Node.js projects.
Add “type”: “module” to package.json
Open your project’s package.json and add the field “type”: “module” at the top level. This tells Node.js to treat all .js files in the project as ES modules, enabling import/export syntax globally. After this change, you must use import instead of require throughout the project, as mixing is not allowed within the same file type.
Use .mjs File Extension
Rename your file from script.js to script.mjs. The .mjs extension tells Node.js to treat the specific file as an ES module regardless of package.json settings. This approach works well for individual scripts without affecting your entire project. Run it with node script.mjs.
Switch to require() Syntax
Replace import express from ‘express’ with const express = require(‘express’). CommonJS require() works in all Node.js versions without configuration. For named imports like import { readFile } from ‘fs’, use const { readFile } = require(‘fs’). This is the simplest fix when you do not need ES module features.
Configure TypeScript Correctly
If you use TypeScript, set “module”: “commonjs” in tsconfig.json to compile import statements into require() calls. Alternatively, set “module”: “ESNext” with “moduleResolution”: “node” and add “type”: “module” to package.json. TypeScript compiles before Node.js executes, so the tsconfig module setting determines the output format.
Fix for Jest and Testing Frameworks
Jest runs in CommonJS mode by default. Add “transform”: {} to your Jest config and use @babel/plugin-transform-modules-commonjs to transpile ES imports. Alternatively, use the experimental –experimental-vm-modules flag with Jest 29+ for native ES module support. Vitest supports ES modules natively and is a drop-in replacement.
Frequently Asked Questions
Can I use both require() and import in the same file?
No. A file is either CommonJS or ES module. In a CommonJS file (.js without “type”: “module”), use require(). In an ES module file (.mjs or .js with “type”: “module”), use import. However, ES modules can dynamically import CommonJS modules using await import(‘./commonjs-file.cjs’).
Does this error affect browser JavaScript?
Browsers always treat script type=”module” as ES modules and regular scripts as classic scripts. The “cannot use import statement outside a module” error in browsers means you loaded a file with a regular script tag instead of script type=”module”. Add type=”module” to your script tag to fix it.








