# “ReferenceError: exports is not defined”

> exports is not defined (in ES module scope). CommonJS code in an ES module: a "type": "module" package, TypeScript set to CommonJS, or a script in the browser.

Canonical: https://app369.com/fix/exports-is-not-defined/
Last updated: 2026-11-08

Node.js stops with ReferenceError: exports is not defined in ES module scope, or the browser console shows exports is not defined (Safari: Can't find variable: exports). The file uses CommonJS, where exports exists, but it is running as an ES module or as a plain browser script, where it does not. Node.js adds a hint naming the package.json that made the file an ES module.

**Short answer**

In Node.js, either rename the file to .cjs so it stays CommonJS, or convert it to export syntax. If the file is TypeScript output, set "module" to "nodenext" in tsconfig.json for Node.js, or "esnext" or "preserve" for code that a bundler or the browser loads. In the browser, never load CommonJS output with a plain script tag: bundle it, or emit ES modules and use type="module".

## **Why exports does not exist here, and the fix**

1. 01 ### **A CommonJS file in a "type": "module" package**
  > “A package.json "type" value of "module" tells Node.js to interpret .js files within that package as using ES module syntax.” [**Node.js, Modules: Packages**](https://nodejs.org/api/packages.html) Adding "type": "module" to package.json turns every .js file into an ES module, including config files and scripts that still use exports or module.exports. Rename those to .cjs, or convert them to export.```
// before: config.js
module.exports = { port: 3000 };

// after: config.cjs (same code), or convert:
export default { port: 3000 };
```
2. 02 ### **TypeScript compiled to CommonJS**
  > “You very likely want "nodenext" for modern Node.js projects and preserve or esnext for code that will be bundled.” [**TypeScript, TSConfig: module**](https://www.typescriptlang.org/tsconfig/#module) With "module": "commonjs", tsc writes exports.x = ... into every file. In a "type": "module" package, or in the browser, that output fails. Pick the module setting for where the code runs.```
// tsconfig.json for Node.js
{ "compilerOptions": { "module": "nodenext" } }

// for code a bundler or the browser loads
{ "compilerOptions": { "module": "esnext" } }
```
3. 03 ### **CommonJS output loaded with a plain script tag** A browser script has no exports object, so the first exports line throws. Serve ES modules with type="module", or run the code through a bundler such as Vite or esbuild that turns CommonJS into browser code.```
<script type="module" src="/js/app.js"></script>
```
4. 04 ### **Importing a CommonJS package from an ES module**
  > “In most cases, the ES module import can be used to load CommonJS modules.” [**Node.js, Modules: ECMAScript modules**](https://nodejs.org/api/esm.html) You do not need to rewrite a CommonJS dependency to use it from an ES module. Import it; Node.js maps module.exports to the default export.```
import pkg from 'some-commonjs-package';
const { helper } = pkg;
```

Checked against the vendor’s docs on November 8, 2026: [Node.js docs: Modules: Packages](https://nodejs.org/api/packages.html); [Node.js docs: ECMAScript modules](https://nodejs.org/api/esm.html); [TypeScript: TSConfig module](https://www.typescriptlang.org/tsconfig/#module).

## **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: [**“require is not defined”**](https://app369.com/fix/require-is-not-defined/) · [**“Cannot use import statement outside a module”**](https://app369.com/fix/node-cannot-use-import-statement-outside-module/) · [**“Unexpected token 'export'”**](https://app369.com/fix/unexpected-token-export/)

Questions

## **More on this error**

<details>

<summary>**Should I just remove "type": "module"? **+</summary>

Only if the project is meant to be CommonJS. Without it, Node.js treats .js files as CommonJS by default. Recent Node.js versions detect import and export syntax and run such a file as an ES module with a warning, but older versions throw, so use .mjs or keep the field. Renaming the few CommonJS files to .cjs is usually the smaller change.

</details>

<details>

<summary>**Is this related to require is not defined? **+</summary>

Yes. Both mean CommonJS code is running as an ES module or in the browser. require, exports and module.exports all exist only in CommonJS.

</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/)