Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

TypeScript does not know what require is

Errorerror TS2304: Cannot find name 'require'.

Short answer

TS2304 on require means TypeScript has no Node.js type declarations in scope. Install @types/node and make sure tsconfig does not exclude it through a types list. If the project runs as an ES module, require does not exist at runtime either, so use import or createRequire.

  • Applies to TypeScript 5.x and 6.0, Node.js 22, npm 10
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Check whether the Node.js type package is installed and which types your tsconfig allows.

    npm ls @types/node
    npx tsc --showConfig | grep -A3 '"types"'
  2. 2Match what you see, then jump to its fix

    1. 1npm ls prints (empty) for @types/node@types/node is not installedFix
    2. 2@types/node is installed but the error staystsconfig lists types and node is not in itFix
    3. 3ReferenceError: require is not defined in ES module scope when the code runsThe file is an ES module, where require does not existFix

What does the error look like?

  • error TS2304: Cannot find name 'require'.
  • Cannot find name 'process'.
  • Cannot find name 'module'.

Cause 1 of 3

@types/node is not installed

1Check

The names require, process and module are declared by the Node.js type package, not by TypeScript itself.

ls node_modules/@types

2Fix

Install the package as a dev dependency. TypeScript finds packages under node_modules/@types by itself.

npm install --save-dev @types/node

3Verify

The compiler passes with no TS2304.

npx tsc --noEmit

Confirmed by typescriptlang.org

Did this fix it?

Cause 2 of 3

tsconfig lists types and node is not in it

1Check

When compilerOptions.types is set, only the listed packages add global names. From TypeScript 6.0 the default is an empty list, so node must be named.

grep -n '"types"\|typeRoots' tsconfig.json

2Fix

Add node to the list.

{
  "compilerOptions": {
    "types": ["node"]
  }
}

3Verify

Compile again.

npx tsc --noEmit

Confirmed by typescriptlang.org

Did this fix it?

Cause 3 of 3

The file is an ES module, where require does not exist

1Check

A file is an ES module with a .mjs extension or when package.json has type set to module. Node.js does not provide require there.

grep -n '"type"' package.json

2Fix

Prefer import. If you must load a CommonJS file, build require with createRequire.

import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
const legacy = require('./legacy.cjs');

3Verify

Run the compiled file and confirm it starts.

node dist/index.js

Confirmed by nodejs.org

Did this fix it?

How do I stop it coming back?

  • Keep @types/node in devDependencies of every Node.js TypeScript project.
  • Match the @types/node major version to the Node.js version you deploy.
  • Decide once between CommonJS and ES modules and set it in package.json.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

cdk synth says --app is required

--app is required either in command-line, in cdk.json or in ~/.cdk.json

The CDK CLI needs a command that runs your app and finds it in three places: the --app option, cdk.json in the project folder, or cdk.json in your home folder.

3 causes, each with a fix

Back end

Mocha or Node cannot find ts-node/register

Error: Cannot find module 'ts-node/register'

Node resolves ts-node/register from your project's node_modules, so the package must be installed there, next to typescript.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.