Maaz Zindani

Loading the index…

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

TypeScript says it cannot find the name 'console'

ErrorCannot find name 'console'.

Short answer

console is not part of the JavaScript language. TypeScript gets it either from the DOM library or from @types/node. If your lib setting leaves out dom and Node's types are missing or filtered out by the types option, console has no declaration. Install @types/node and list node in types.

  • Applies to TypeScript 5, @types/node 22, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the settings TypeScript really uses, then look at lib and types in the output.

    npx tsc --showConfig
  2. 2Match what you see, then jump to its fix

    1. 1The same file also reports Cannot find name 'process' or 'require', and node_modules/@types/node does not exist.@types/node is not installedFix
    2. 2tsconfig.json has a types array, such as ["jest"], that does not include node.The types option hides @types/nodeFix
    3. 3tsconfig.json sets lib without "dom", while the code is front-end code.A custom lib drops the DOM declarations, and the code runs in a browserFix

What does the error look like?

  • Cannot find name 'console'.

Cause 1 of 3

@types/node is not installed

1Check

Check that the package is a dependency of the project.

npm ls @types/node

2Fix

Install the Node.js type declarations as a dev dependency.

npm install -D @types/node

3Verify

Run the compiler without emitting files.

npx tsc --noEmit

Confirmed by typescriptlang.org

Did this fix it?

Cause 2 of 3

The types option hides @types/node

1Check

When types is set, only the listed packages add globals. Others under node_modules/@types are ignored.

grep -n -A3 '"types"' tsconfig.json

2Fix

Add node to the list. From TypeScript 6.0 the default for types is an empty list, so a project upgraded to 6.0 needs this entry even if it had no types option before.

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

3Verify

Compile again.

npx tsc --noEmit

Confirmed by typescriptlang.org

Did this fix it?

Cause 3 of 3

A custom lib drops the DOM declarations, and the code runs in a browser

1Check

TypeScript includes browser definitions such as document by default. A lib list replaces that default.

grep -n '"lib"' tsconfig.json

2Fix

Add dom to lib for browser code. For code that only runs in Node.js, leave dom out and use @types/node instead.

{
  "compilerOptions": {
    "lib": ["ES2023", "DOM"]
  }
}

3Verify

Compile again.

npx tsc --noEmit

Confirmed by typescriptlang.org

Did this fix it?

How do I stop it coming back?

  • Keep @types/node in devDependencies of every package that uses Node.js globals.
  • After upgrading TypeScript, run tsc --noEmit in CI so a changed default shows up at once.
  • Do not add dom to lib in a server project just to silence the error.

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.