This TypeScript error (TS2304) occurs when using CommonJS exports without proper configuration. The fix involves installing @types/node, configuring tsconfig.json with CommonJS module settings, or using proper ES module syntax.
TypeScript error TS2304 "Cannot find name 'exports'" happens when the compiler doesn't recognize the `exports` keyword. This is a type system problem, not a runtime issue. By default, TypeScript assumes you're using ES modules (import/export syntax) and doesn't know about CommonJS's `exports` object. When you try to use CommonJS syntax like `module.exports = ...` or `exports.something = ...`, TypeScript can't find the type definition for the global `exports` variable, resulting in this error. This error is common when: - You're migrating a Node.js project to TypeScript - You have a mixed module setup (some ES modules, some CommonJS) - You're setting up a new TypeScript project for Node.js without proper configuration
The most common fix is installing Node.js type definitions:
npm install --save-dev @types/nodeOr with yarn:
yarn add --dev @types/nodeThis provides TypeScript with type definitions for all Node.js globals, including exports, require, and module.
Ensure your tsconfig.json is set up for CommonJS modules. Edit or create tsconfig.json in your project root:
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"moduleResolution": "node",
"lib": ["ES2020"],
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"strict": true
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
}The key settings are:
- "module": "commonjs" - tells TypeScript you're using CommonJS
- "moduleResolution": "node" - uses Node.js module resolution
Ensure you're using CommonJS syntax consistently. There are several valid patterns:
Pattern 1: Direct module.exports
// myModule.ts
const doSomething = () => "done";
module.exports = doSomething;Pattern 2: Named exports with exports object
// utils.ts
export const add = (a: number, b: number): number => a + b;
export const subtract = (a: number, b: number): number => a - b;Pattern 3: CommonJS exports object (requires @types/node)
// oldStyle.ts
exports.add = (a: number, b: number): number => a + b;
exports.subtract = (a: number, b: number): number => a - b;After installing @types/node or updating tsconfig.json, restart your TypeScript language server:
In VS Code:
1. Open the Command Palette (Ctrl+Shift+P or Cmd+Shift+P)
2. Search for "TypeScript: Restart TS Server"
3. Press Enter
In other editors:
- Close and reopen the file
- Restart the IDE
- Restart the TypeScript language server plugin
This forces the IDE to re-read the type definitions and configuration.
If restarting doesn't help, check that Node.js types are being loaded correctly:
# Verify @types/node is installed
npm list @types/node
# Reinstall if necessary
npm install --save-dev @types/node@latestYou can also check your package.json to confirm the dependency is listed:
{
"devDependencies": {
"@types/node": "^20.0.0",
"typescript": "^5.0.0"
}
}### ES Modules vs CommonJS
If you're starting a new project, consider using ES modules instead:
ES Module approach (recommended for new projects):
// tsconfig.json
{
"compilerOptions": {
"module": "es2020",
"moduleResolution": "node"
}
}
// myModule.ts
export const doSomething = () => "done";
export default { doSomething };This avoids the entire TS2304 issue and is the modern standard.
### Mixed Module Systems
If you need to consume CommonJS modules from ES module code:
// ES module importing CommonJS
import oldStyle = require("./oldModule");
// or
import * as oldStyle from "./oldModule";### TypeScript Node (ts-node)
If you're using ts-node to run TypeScript directly, ensure your ts-node configuration matches your module setup:
{
"ts-node": {
"compilerOptions": {
"module": "commonjs"
}
}
}### Prisma and ORM Libraries
If you're seeing this error while using Prisma or other libraries, they often expect CommonJS. Keep your configuration as shown in Step 2 above.
Function expression requires a return type
Function expression requires a return type
Value of type 'string | undefined' is not iterable
How to fix "Value is not iterable" in TypeScript
Type 'undefined' is not assignable to type 'string'
How to fix "Type undefined is not assignable to type string" in TypeScript
Type narrowing from typeof check produces 'never'
How to fix "Type narrowing produces never" in TypeScript
Type parameter 'T' has conflicting constraints
How to fix "Type parameter has conflicting constraints" in TypeScript