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
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
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 };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
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" } }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>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
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 : Node.js docs: Modules: Packages; Node.js docs: ECMAScript modules; TypeScript: TSConfig module.
Rather hand it over?
Fixed scope, fixed price, paid upfront. The work happens in writing; no call needed.
- from $450App Fix TicketOne reproducible bug in an existing mobile or web app, diagnosed and fixed.See scope and start
- from $450AI-Built App RescueA 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
Related: “require is not defined” · “Cannot use import statement outside a module” · “Unexpected token 'export'”