How to fix "SyntaxError: Unexpected token 'export'"
Tested: we reproduced this error and confirmed the fix on Node 22.23, Jest 29.7, Vitest 5.1, Babel 7 and headless Chrome on Ubuntu 24.04, November 29, 2026.
The file uses export, but something is loading it as a classic script or as CommonJS. The fix depends on what loaded it:
| Where you see it | Fix |
|---|---|
| Browser console | Load the file with <script type="module" src="..."> |
| Jest | Transform the ESM package with transformIgnorePatterns, or use Jest’s ESM mode |
| Vitest | Inline the package with server.deps.inline |
| Node.js | Add "type": "module" to package.json, rename the file to .mjs or update Node |
eval, new Function() or a code runner |
Run the code as a module, or remove the export |
We reproduced each case with Node 22.23, Jest 29.7 and headless Chrome. Vitest 5.1 didn’t throw the error in our tests, so that fix comes from Vitest’s documentation.
In the browser: add type=“module”
A file with export const a = 1; loaded with a plain <script src="lib.js"> tag gave Uncaught SyntaxError: Unexpected token 'export' in Chrome, and the script never ran. With type="module" the same file ran:
<script type="module" src="lib.js"></script>
Module scripts must be served over HTTP. When we opened the same page straight from disk (file://), Chrome blocked the module with a CORS error, so use a local server such as npx serve or python3 -m http.server. Browsers don’t add file extensions for you, so imports need the full file name (./utils.js, not ./utils) unless a bundler handles them.
In Jest: transform the ESM package
With recent Node versions handling most cases, Jest is a common source of the error. Jest runs test files as CommonJS and by default skips node_modules when transforming code. A test that required an ESM-only package failed with:
Jest encountered an unexpected token
...
By default "node_modules" folder is ignored by transformers.
...
/tmp/ute/node_modules/esm-dep/index.js:1
SyntaxError: Unexpected token 'export'
The path under “Details” names the package to fix. Tell Jest to transform it by excluding it from the ignore pattern:
// jest.config.js
module.exports = {
transformIgnorePatterns: ['/node_modules/(?!esm-dep)'],
};
For more than one package, list them: '/node_modules/(?!(pkg-a|pkg-b)/)'. Jest also needs a transformer that converts ES modules. With Babel 7 we added babel.config.json:
{ "presets": [["@babel/preset-env", { "targets": { "node": "current" } }]] }
With both in place the test passed. Without @babel/preset-env installed, Jest stopped with Cannot find module '@babel/preset-env' instead. If npm refuses to install it with an ERESOLVE error, your @babel/core and preset-env major versions don’t match. Pinning both to 7 fixed that in our test, and our ERESOLVE guide explains the error.
TypeScript projects need the same transformIgnorePatterns change, with a transformer that also handles the package’s JavaScript files. Jest’s ECMAScript modules guide covers running tests as native ESM instead, which avoids the transform but is still marked experimental.
In Vitest: inline the package
Vitest passes most packages in node_modules straight to Node instead of transforming them, so the error shows up when Node can’t load an ES module on its own. On Vitest 5.1.2 and Node 22.23 we couldn’t trigger it. A test that imported an ESM-only package passed, and so did one that imported a CommonJS package which require()s it, because current Node can load an ES module from require(). Older Node versions that can’t will still throw.
If you still get the error, Vitest’s server.deps.inline option makes Vite transform the package instead of handing it to Node. Name the package that fails, or the package that imports it:
// vitest.config.js
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
server: { deps: { inline: ['package-name'] } },
},
})
Vitest 5.1.2 accepted this config and our test still passed with it, but we couldn’t watch it fix the error because the error never appeared. A Vitest maintainer gives the same advice in this GitHub issue: inline the dependency that imports the failing package.
In Node.js: tell Node the file is a module
Recent Node versions usually work this out themselves. The Node 22.7.0 release notes say module syntax detection is on by default, and the 22.12.0 notes say require() can load ES modules without a flag. In our Node 22.23 test, node lib.js on a file with export ran, with this warning:
[MODULE_TYPELESS_PACKAGE_JSON] Warning: Module type of file:///tmp/ute/lib.js is not specified and it doesn't parse as CommonJS.
Reparsing as ES module because module syntax was detected.
A CommonJS file could also require() it. To make the module type explicit (and remove the warning), do one of these:
- Add
"type": "module"to the nearestpackage.json. - Rename the file to
.mjs.
If you’re stuck on an older Node version without detection, or a tool that uses its own loader, these same two changes fix the error there too. Switching to "type": "module" means .js files can no longer use require. A file that mixed both failed in our test with ReferenceError: require is not defined in ES module scope. Use import throughout, or rename CommonJS files to .cjs.
In eval, new Function or a code runner
new Function("export const x=1") threw SyntaxError: Unexpected token 'export' even on Node 22.23, because function bodies and eval are never modules. Online code runners and some editor “Run” buttons work the same way. Remove the export keyword, or put the code in a .mjs file and run it with node.
The reverse problem, import failing in a CommonJS context, has its own guide: Cannot use import statement outside a module.
FAQ
What does Unexpected token 'export' mean?
The JavaScript engine reached the word export, which only exists in ES modules, while it was reading the file as an ordinary script or as CommonJS. The code itself is usually fine. Whatever loaded it needs to be told it's a module, or the file needs converting first.
How do I fix Unexpected token 'export' in Jest?
Jest runs tests as CommonJS and by default doesn't transform files in node_modules. If an ESM-only package causes it, add that package to an exception in transformIgnorePatterns and make sure Babel (or ts-jest) is set up to transform it. Jest's native ESM mode is the other option.
Does Node.js still throw Unexpected token 'export'?
Less often. Node 22.7 turned on module syntax detection by default, so a .js file with export syntax and no "type" field runs as an ES module with a warning. In Node 22.23 we only got the error from code evaluated outside a module, such as new Function(). Older Node versions and tools with their own loaders still throw it.
