GrepBoard/Manuals
605 manuals ·

Fix SyntaxError: Cannot use import statement outside a module

·Tested on Node.js 18.20.8 · 20.20.2 · 22.23.4 · 24.22.1 (Docker node:*-slim) · 2026-10·Test scope ↓·5 steps

Cannot use import statement outside a module means Node loaded the file as CommonJS. Check package.json type and Node version; fix ERR_REQUIRE_ESM too.

output
/srv/app/app.js:1import { hello } from './lib.mjs';^^^^^^ SyntaxError: Cannot use import statement outside a module

Cannot use import statement outside a module means Node.js loaded a file containing import as CommonJS. Adding "type": "module" to the nearest package.json, or renaming the file to .mjs, makes Node load it as an ES module. The same file behaves differently depending on the Node version and the type value, so find out which case you have first.

[1] Check the Node version and the type in the nearest package.json.

console
$ node -v$ npm pkg get type

The same app.js (a single import) run in the official images for four versions:

Condition18.20.820.20.2 · 22.23.4 · 24.22.1
no package.jsonSyntaxErrorruns
package.json without typeSyntaxErrorruns + warning
"type": "commonjs"SyntaxErrorSyntaxError
"type": "module"runsruns

The Node docs' "Syntax detection" is on by default since 22.7.0: with no type, Node re-parses the file as an ES module when it sees module syntax (20.20.2 behaved the same in the test). So on a current release this error almost always means "type": "commonjs" is set, or you are on an old release such as Node 18. With a package.json that has no type, Node 22 added this warning:

output
hello from lib.mjs(node:31) [MODULE_TYPELESS_PACKAGE_JSON] Warning: Module type of file:///srv/app/app.js is not specified and it doesn't parse as CommonJS.Reparsing as ES module because module syntax was detected. This incurs a performance overhead.To eliminate this warning, add "type": "module" to /srv/app/package.json.

[2] If the whole project uses import, set type to module.

console
$ npm pkg set type=module; grep -n type package.json
output
12:  "type": "module"

This applies to every .js under that package.json. Any .js that still uses require(), module.exports or __dirname now fails the other way:

output
ReferenceError: require is not defined in ES module scope, you can use import insteadThis file is being treated as an ES module because it has a '.js' file extension and '/srv/app/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.

Rename those files to .cjs, as the message says. If many files would need renaming, the smaller change is the reverse: leave type alone and rename only the files that use import to .mjs. To undo, run npm pkg delete type (or npm pkg set type=commonjs if that was the original value).

[3] If require() of an ES module fails with ERR_REQUIRE_ESM, check the Node version first. Node 18 blocks it like this:

output
Error [ERR_REQUIRE_ESM]: require() of ES Module /srv/app/lib.mjs not supported.Instead change the require of /srv/app/lib.mjs to a dynamic import() which is available in all CommonJS modules.

The same main.cjs ran unchanged on 20.20.2, 22.23.4 and 24.22.1. Per the Node docs, loading ES modules with require() became unflagged in 22.12.0 and 20.19.0. Like 18.x, 20.x is already end-of-life (20.x ended on 2026-04-30 — Node.js release schedule), so if an ESM-only package triggers this error, upgrading to the latest patch of a supported line, Node 22 or 24, is the shortest path. If you only need to know where the feature starts, the boundaries are 20.19.0 on 20.x and 22.12.0 on 22.x (it is not on by default in 21.x or 22.0–22.11). If you cannot upgrade, use import() from [5].

[4] On a current release, ERR_REQUIRE_ASYNC_MODULE means the module you require (or something it imports) has a top-level await.

output
Error [ERR_REQUIRE_ASYNC_MODULE]: require() cannot be used on an ESM graph with top-level await. Use import() instead. To see where the top-level await comes from, use --experimental-print-required-tla.

Upgrading does not fix this one; the docs also say to use import() here. If you do not know which file has the await, run once with --experimental-print-required-tla as the message suggests.

[5] For code that has to stay CommonJS, switch to dynamic import(). It works on every version tested.

main-dyn.cjs
import('./tla.mjs').then((m) => console.log(m.default));
console
$ node main-dyn.cjs
output
tla

import() returns a Promise, so move the code that uses the value into then or an async function. Take a default export from m.default.

For npm errors during install, see npm ERR! code ERESOLVE and npm install -g EACCES.

#nodejs#esm#commonjs#err-require-esm