Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Unexpected token 'export' when you build or run JavaScript

ErrorSyntaxError: 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.

  • Applies to webpack 5, babel-loader 9, Node.js 22, Node.js 24
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 40
  2. 2Match what you see, then jump to its fix

    1. 1The failing path is inside node_modules and your babel-loader rule has exclude: /node_modules/A dependency ships modern syntax and babel-loader skips node_modulesFix
    2. 2The error comes from node file.js or from a script, and the file uses exportNode.js loads your own file as CommonJSFix

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.js

2Fix

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 production

Confirmed by webpack.js.org

Did this fix it?

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.json

2Fix

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.js

Confirmed by nodejs.org

Did this fix it?

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:

Related fixes

Web 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 fix

Web 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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.