How to fix "SyntaxError: Cannot use import statement outside a module"
Tested: we reproduced this error and confirmed the fix on Node 18, 20 and 22.23, Jest 29.7 and 30.5 and Chrome 153 on Ubuntu 24.04, November 29, 2026.
The fix is to tell Node your file is an ES module. Add "type": "module" to the nearest package.json, or rename the file to .mjs:
{
"name": "my-app",
"type": "module"
}
Both fixes worked in our tests. The error means whatever is loading your code (Node, Jest, a test runner or the browser) is treating it as an old-style script, where import isn’t allowed.
Which setup are you in?
We reproduced this error in four setups. Find yours:
| Where you see it | Why | Fix |
|---|---|---|
Node 18 or older, .js file, no "type" in package.json |
Node 18 treats .js as CommonJS |
"type": "module" or .mjs |
Any Node version with "type": "commonjs" |
You told Node it’s CommonJS | Remove that line, set "module" or rename to .mjs |
| Jest 29 | Jest loads tests as CommonJS by default | Add a Babel transform or run Jest in ESM mode (below) |
| The browser console | <script> without type="module" |
<script type="module" src="app.js"> |
One thing that surprised us: on Node 22.23 and on current Node 20 a .js file with import and no "type" field runs anyway. Node detects the syntax and prints a MODULE_TYPELESS_PACKAGE_JSON warning instead of failing. On Node 18 the same file throws the error. So if it works on your machine but fails in CI or for a teammate, compare node -v.
Fix it in Node
- Open the
package.jsonclosest to the file that failed. - Add
"type": "module". - Run the file again.
If you can’t change the whole package (some files still use require), rename only the files that use import to .mjs. Node treats a .mjs file as an ES module whatever package.json says, and .cjs as CommonJS. Relative imports between ES modules need the extension, so write import { add } from './math.js', not './math'. Leaving it off gives ERR_MODULE_NOT_FOUND with the hint Did you mean to import "./math.js"?.
Fix it in Jest
Jest 29 printed Jest encountered an unexpected token followed by this error. Two fixes passed in our test:
Run Jest in ESM mode. Set "type": "module" and start Jest through Node with the experimental flag:
{
"type": "module",
"scripts": { "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js" }
}
Or transform with Babel. Install @babel/core@7, @babel/preset-env@7 and babel-jest, then add babel.config.json:
{ "presets": [["@babel/preset-env", { "targets": { "node": "current" } }]] }
Match the Babel major version to your Jest: installing @babel/preset-env without a version next to Jest 29 pulled in a Babel 8 core and failed with npm ERR! code ERESOLVE in our test. Jest 30 shows a different message for the same problem (“Must use import to load ES Module”), and the same fixes apply.
Fix it in the browser
Chrome 153 showed Uncaught SyntaxError: Cannot use import statement outside a module for this tag:
<script src="app.js"></script>
Adding the module type fixed it:
<script type="module" src="app.js"></script>
Module scripts are loaded over HTTP with CORS rules, so open the page through a local server (for example npx serve) rather than double-clicking the file.
Then you get “X is not defined”
Variables declared in a module script stay inside that module; they don’t become globals. If an older, non-module script on the page expects a global from the file you just switched to type="module", it now fails with Uncaught ReferenceError: X is not defined. That’s the follow-up problem in the top Stack Overflow question for this error. Import what you need inside a module script instead:
<script type="module">
import ms from './milsymbol.js';
// use ms here
</script>
We didn’t reproduce this follow-up error.
Confirm it worked
Run the same command again. Node should print your output with no SyntaxError, and with "type": "module" set you shouldn’t see the MODULE_TYPELESS_PACKAGE_JSON warning either. Node explains how it decides between the two module systems in its packages documentation.
FAQ
Should I use ES modules or CommonJS?
For new code, ES modules (import/export) with "type": "module" in package.json. CommonJS (require) is still fine for existing projects. Pick one per package so Node doesn't have to guess.
Why does my code run with node but fail in Jest?
Jest 29 loads test files as CommonJS unless you add a transform or run it with --experimental-vm-modules. Node itself is more forgiving: Node 22 detects import syntax on its own.
Why does this error appear in the browser?
The <script> tag loading your file has no type="module". Add it, or bundle your code first.
Can I use import and require in the same file?
No. In an ES module require is not defined, and Node 22 says so: ReferenceError: require is not defined in ES module scope. Use import throughout, or createRequire from node:module if you must load a CommonJS-only file.
