Node.js “Cannot use import statement outside a module”: pick a module system
Tell Node.js the file is an ES module with "type": "module" or an .mjs extension, or convert the file to require().
Node.js parsed your file as CommonJS. Add "type": "module" to package.json, rename the file to .mjs, or rewrite the imports as require() calls.
Try these fixes in order
Check how Node.js interprets the file
Find the package.json nearest to the failing file. If its "type" field is missing or "commonjs", Node.js parses .js files as CommonJS and import statements are a syntax error.
node -p "require('./package.json').type || '(no type field)'"Declare the file as an ES module
If the project should be ES modules, add "type": "module" to package.json. If only this file should be ESM, rename it to .mjs instead, which is always loaded as an ES module regardless of package.json.
mv app.js app.mjsOr convert the file to CommonJS
If the project should stay CommonJS, convert the file: replace import with require() and export with module.exports. Mixed files need one consistent system.
Rerun and fix the follow-on errors
Rerun the entry point. If a new error names require or __dirname as undefined, the file is now ESM and those CommonJS globals need converting too.
node app.mjsInterpret what you see
The failing file is .js and package.json lacks "type": "module"
What it suggests: Node.js treats .js files as CommonJS unless the nearest package.json declares "type": "module" or the file uses the .mjs extension.
Next check: Open the nearest package.json and check for a top-level "type" field; check the file extension of the failing file.
import works in one file but fails when the same code is required from another
What it suggests: The entry point was loaded as CommonJS (for example through require() or a loader expecting CJS) while its source uses import.
Next check: Check how the file is launched and whether a parent file require()s it instead of importing it.
The error changes to "require is not defined in ES module scope"
What it suggests: Renaming to .mjs fixes the parse error but the file still calls require(), which does not exist in ES module scope.
Next check: After switching the module system, convert require/module.exports to import/export or use createRequire for the exceptions.
Why this happens
- A .js file uses import syntax while the nearest package.json has no "type": "module", so Node.js parses it as CommonJS.
- A project mixes module systems without per-file extensions, so some files are interpreted under the wrong system.
Still not working?
Do not flip "type" to "module" in a package full of require() calls to fix one file; that just moves the error to every other file ("require is not defined in ES module scope"). Choose one module system per package deliberately, or use explicit .mjs/.cjs extensions for the exceptions.
Official sources
These steps are based on the linked documentation and general diagnostics. BugMedic has not independently tested every product and system configuration. Review commands before running them.