How to fix Cannot find name 'exports' in TypeScript

TypeScriptBEGINNERMEDIUMUpdated Dec 8, 2025ยท 112 views

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.

Quick fix

Fixes TS2304

npm install --save-dev @types/node
  1. Install @types/node
  2. Configure tsconfig.json for CommonJS
  3. Verify your export syntax
  4. Reload your IDE or editor
typescript โ€” error
npm install --save-dev @types/node
Cannot find name 'exports'
shell
npm install --save-dev @types/node

What this error means

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

Typical symptoms

  • Compiler shows 'Cannot find name exports' when using module.exports or exports
  • Error appears on the line where you try to export: exports.something = ...
  • The error is TS2304 (generic 'cannot find name' error)
  • Code works fine in plain Node.js but fails TypeScript type checking
  • IDE shows red squiggly lines under 'exports' in the editor

Common causes

  1. @types/node is not installed in your project (most common)
  2. tsconfig.json is missing or has incorrect module resolution settings
  3. tsconfig.json module option is set to 'es2015', 'esnext', or other ES-only formats instead of 'commonjs'
  4. Mixing CommonJS (require/exports) with ES module configuration
  5. Using a bundler or tool that changes the default module system
  6. The TypeScript version is very old and doesn't include full Node.js type support

How to fix "Cannot find name 'exports'"

1Install @types/node

The most common fix is installing Node.js type definitions:

bash
npm install --save-dev @types/node

Or with yarn:

bash
yarn add --dev @types/node

This provides TypeScript with type definitions for all Node.js globals, including exports, require, and module.

2Configure tsconfig.json for CommonJS

Ensure your tsconfig.json is set up for CommonJS modules. Edit or create tsconfig.json in your project root:

json
{
  "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

3Verify your export syntax

Ensure you're using CommonJS syntax consistently. There are several valid patterns:

Pattern 1: Direct module.exports

typescript
// myModule.ts
const doSomething = () => "done";

module.exports = doSomething;

Pattern 2: Named exports with exports object

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

typescript
// oldStyle.ts
exports.add = (a: number, b: number): number => a + b;
exports.subtract = (a: number, b: number): number => a - b;
4Reload your IDE or editor

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.

5Check for Node.js type conflicts

If restarting doesn't help, check that Node.js types are being loaded correctly:

bash
# Verify @types/node is installed
npm list @types/node

# Reinstall if necessary
npm install --save-dev @types/node@latest

You can also check your package.json to confirm the dependency is listed:

json
{
  "devDependencies": {
    "@types/node": "^20.0.0",
    "typescript": "^5.0.0"
  }
}

Advanced notes

### ES Modules vs CommonJS

If you're starting a new project, consider using ES modules instead:

ES Module approach (recommended for new projects):

typescript
// 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:

typescript
// 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:

json
{
  "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.

Frequently asked questions

What does "TS2304" mean?
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.
How do I fix "TS2304"?
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.
What causes "TS2304"?
@types/node is not installed in your project (most common) tsconfig.json is missing or has incorrect module resolution settings tsconfig.json module option is set to 'es2015', 'esnext', or other ES-only formats instead of 'commonjs'
Is the "TS2304" error serious?
It is a medium-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading