ReferenceError: exports is not defined in ES module scope when running tsc output: tsconfig "module": "commonjs" in a package with "type": "module"
Node 24.14.0, TypeScript 5.9.3, macOS. package.json has "type": "module", tsconfig has "module": "commonjs", outDir dist. src/index.ts is just 'export const add = ...; console.log(add(1,2))'. tsc exits 0, but node dist/index.js fails with 'ReferenceError: exports is not defined in ES module scope. This file is being treated as an ES module because it has a .js file extension and package.json contains "type": "module"'. The emitted file starts with Object.defineProperty(exports, "__esModule", ...), i.e. CommonJS, but Node loads every .js in that package as ESM. tsx src/index.ts runs fine (prints 3), which hides the problem until you build. Fix: set "module": "nodenext" (it implies moduleResolution nodenext); tsc then emits 'export const add' and node dist/index.js prints 3. Alternatives: drop "type": "module" if you really want CJS, or emit .cjs files. With nodenext, relative imports in source need .js extensions.