Unexpected token 'export' when you build or run JavaScript
SyntaxError: Unexpected token 'export'Short answer
This error means a JavaScript engine met an ES module export statement where it expected CommonJS or older syntax. Two cases cover it. A package in node_modules ships modern syntax and your babel-loader rule skips it, or Node.js treats your own file as CommonJS. Find which file throws, then transpile that package or mark the file as a module.
Fix it fast
1Find out which cause you have
Read the first stack line or the module path in the build output. It names the file that holds the export statement. If the path contains node_modules, start with the first cause. If it is your own file, start with the second.
npx webpack --stats-error-details 2>&1 | head -n 402Match what you see, then jump to its fix
What does the error look like?
SyntaxError: Unexpected token 'export'Uncaught SyntaxError: Unexpected token 'export'
Cause 1 of 2
A dependency ships modern syntax and babel-loader skips node_modules
1Check
Open your webpack config and look at the rule that uses babel-loader. A plain exclude of node_modules means no package is ever transpiled, even one that only ships ES module syntax.
grep -n -B2 -A6 "babel-loader" webpack.config.js2Fix
Change the exclude so that node_modules is skipped except for the named package. The loader docs show this with an exclude object that has and and not lists.
{
test: /\.(?:js|mjs|cjs)$/,
exclude: {
and: [/node_modules/],
not: [/some-esm-package/],
},
use: {
loader: 'babel-loader',
options: { presets: [['@babel/preset-env']] },
},
}3Verify
Run the build again. The error for that package should be gone.
npx webpack --mode productionConfirmed by webpack.js.org
Cause 2 of 2
Node.js loads your own file as CommonJS
1Check
Node.js reads a .js file as an ES module only when the nearest package.json has the field type set to module. Without it the file is CommonJS, and export is a syntax error. Look for that field.
node --version
grep -n "\"type\"" package.json2Fix
Either set the type field to module in package.json, or rename the file to .mjs, which is always an ES module. Keep a file with require() as .cjs if the rest of the project is a module.
// package.json
{
"type": "module"
}3Verify
Run the file again.
node my-app.jsConfirmed by nodejs.org
How do I stop it coming back?
- Keep the exclude rule for node_modules narrow, and add a package to the allow list only when its build output fails.
- Set the type field in package.json on purpose in every project, so Node.js never has to guess.
- Read the first error line of a build before changing config; it names the file.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
Related fixes
Web apps
ChunkLoadError: Loading chunk failed after a Next.js deploy
Missing assets: the client requests JavaScript or CSS files that no longer exist on the server
A browser asked for a JavaScript file that the server no longer has or that another instance never built.
2 causes, each with a fixWeb apps
FATAL ERROR: heap out of memory during a webpack or npm build
FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
The build process hit V8's old-space limit and stopped.
3 causes, each with a fixWeb apps
A React component crashes while destructuring props or state
TypeError: Cannot destructure property 'b' of 'null' as it is null.
JavaScript throws this when the right side of a destructuring pattern is undefined or null.
3 causes, each with a fixWeb apps
Jest fails to parse a React Native or Expo dependency
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax
Jest does not run Babel on node_modules by default, and many React Native packages ship untranspiled source.
3 causes, each with a fixWeb apps
Sentry: Non-Error promise rejection captured with value in a React app
Non-Error exception (or promise rejection) captured with keys: x, y, z
Sentry raises this event when something that is not an Error object is thrown or used to reject a promise, for example a plain object from an API.
3 causes, each with a fixWeb apps
TypeScript says a JavaScript module implicitly has an any type
Could not find a declaration file for module 'react-materialize'. 'path/to/module-name.js' implicitly has an 'any' type.
TypeScript found the JavaScript file but no types for it, and noImplicitAny turns that into an error.
2 causes, each with a fix