# JavaScript: “SyntaxError: Unexpected token 'export'”

> SyntaxError: Unexpected token 'export' in the browser, Node.js or Jest. ES module code ran as a classic script or CommonJS. Use type="module" or transform it.

Canonical: https://app369.com/fix/unexpected-token-export/
Last updated: 2026-11-08

A page, a Node.js script or a Jest test fails with SyntaxError: Unexpected token 'export'. The file uses ES module syntax (export, often with import), but it was loaded as a classic browser script or as CommonJS, where export is not a keyword. Where it happens tells you which side to change.

**Short answer**

In the browser, load the file with script type="module". In Node.js, make the file an ES module: .mjs, or "type": "module" in package.json (or switch the file to module.exports if the project is CommonJS). In Jest, the file is usually a package in node_modules that ships only ES modules, which Jest does not transform by default: add it to transformIgnorePatterns as an exception, or use Jest’s ES module support. In Next.js, list the package in transpilePackages.

## **Where the file was loaded, and the fix**

1. 01 ### **In the browser: a classic script tag**
  > “If this module is referenced from a <script> tag, you can use the type="module" attribute.” [**MDN, JavaScript modules**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) A script tag without type="module" runs the file as a classic script. Mark it as a module, and import its dependencies with paths the browser can load.```
<script type="module" src="/js/app.js"></script>
```
2. 02 ### **In Node.js: the file runs as CommonJS**
  > “Files with a .js extension when the nearest parent package.json file contains a top-level "type" field with a value of "module".” [**Node.js, Packages: determining module system**](https://nodejs.org/api/packages.html) Node.js treats .cjs files, and .js files under "type": "commonjs", as CommonJS. Node.js 22.7 and 20.19 and newer detect ES module syntax in files with no marker, so the error mostly appears in explicit CommonJS and in older versions. Pick one module system per file: rename to .mjs, set "type": "module", or convert export to module.exports.```
// package.json
{ "type": "module" }
```
3. 03 ### **In Jest: an ES module package in node_modules**
  > “If the file path matches any of the patterns, it will not be transformed.” [**Jest, Configuration: transformIgnorePatterns**](https://jestjs.io/docs/configuration) Jest transforms your code but skips node_modules by default, so a dependency that ships only ES modules reaches Node.js untransformed. Let Jest transform that package by excluding it from the ignore pattern. This assumes the default babel-jest transform; with next/jest, which sets its own ignore patterns, override transformIgnorePatterns on the config it returns.```
// jest.config.js
module.exports = {
  transformIgnorePatterns: ['/node_modules/(?!(nanoid|uuid)/)'],
};
```
4. 04 ### **In Next.js or another bundler** A package that ships untranspiled ES module syntax can hit the same error. In Next.js, add the package to transpilePackages; in other bundlers, include it in the transpile step.```
// next.config.mjs
export default { transpilePackages: ['some-esm-package'] };
```

Checked against the vendor’s docs on November 8, 2026: [MDN: JavaScript modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules); [Node.js: Packages (module system)](https://nodejs.org/api/packages.html); [Jest: Configuration (transformIgnorePatterns)](https://jestjs.io/docs/configuration).

## **Rather hand it over?**

Fixed scope, fixed price, paid upfront. The work happens in writing; no call needed.

- [from $450**App Fix Ticket**One reproducible bug in an existing mobile or web app, diagnosed and fixed.**See scope and start**](https://app369.com/fixed-price/flutter-firebase-bug-fix/)
- [from $450**AI-Built App Rescue**A Lovable, Bolt or Cursor app checked end to end in 3 days, then its worst problems fixed at a fixed price.****No issues worth fixing, no charge.**** If the Launch Check finds nothing rated medium or higher, Simon refunds the full $450. You keep the report either way. This applies to the first 10 Launch Checks bought after October 30, 2026.**See scope and start**](https://app369.com/fixed-price/ai-app-rescue/)

Related: [**Node.js “Cannot use import statement outside a module”**](https://app369.com/fix/node-cannot-use-import-statement-outside-module/) · [**Node.js ERR_REQUIRE_ESM**](https://app369.com/fix/node-err-require-esm/) · [**“SyntaxError: Unexpected identifier”**](https://app369.com/fix/unexpected-identifier/)

Questions

## **More on this error**

<details>

<summary>**Is this the same as Cannot use import statement outside a module? **+</summary>

Same cause, different keyword. Whichever of import or export the engine reaches first in a non-module file triggers its own message. The fixes are the same.

</details>

<details>

<summary>**Should I just add Babel? **+</summary>

Only if the project already uses it. In most projects the smaller fix is the module type: type="module", "type": "module" or a transform exception for one package.

</details>

<details>

<summary>**Can you fix it for me? **+</summary>

If one bug is the whole problem, the App Fix Ticket covers it: a written diagnosis first, then the fix. If the app has more going on, the Launch Check is a written check of the whole app. Each issue found is rated critical, high, medium or low, with where it is and how it was confirmed. No issues worth fixing, no charge. If the Launch Check finds nothing rated medium or higher, Simon refunds the full $450. You keep the report either way. This applies to the first 10 Launch Checks bought after October 30, 2026.

</details>

A different error?

## **Describe it and get a written quote**

Send the error and what you were doing. You get a written scope, a fixed price and a timeline; Simon replies by email, usually within one business day.

[**Describe your problem **→](https://app369.com/contact/) [**All fixes**](https://app369.com/fix/)