TypeScript says it cannot find the name 'console'
Cannot 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.
Fix it fast
1Find out which cause you have
Print the settings TypeScript really uses, then look at lib and types in the output.
npx tsc --showConfig2Match what you see, then jump to its fix
- 1
The same file also reports Cannot find name 'process' or 'require', and node_modules/@types/node does not exist.@types/node is not installedFix - 2
tsconfig.json has a types array, such as ["jest"], that does not include node.The types option hides @types/nodeFix - 3
tsconfig.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
- 1
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/node2Fix
Install the Node.js type declarations as a dev dependency.
npm install -D @types/node3Verify
Run the compiler without emitting files.
npx tsc --noEmitConfirmed by typescriptlang.org
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.json2Fix
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 --noEmitConfirmed by typescriptlang.org
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.json2Fix
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 --noEmitConfirmed by typescriptlang.org
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Back end
TypeScript does not know what require is
Cannot find name 'process'.
TS2304 on require means TypeScript has no Node.js type declarations in scope.
3 causes, each with a fixBack end
TypeScript cannot find '@angular/platform-browser' in an Angular project
Cannot find module '@angular/platform-browser' or its corresponding type declarations.
TS2307 means TypeScript could not turn the import into a file.
3 causes, each with a fixBack end
Angular build fails with TS2707 or "has no exported member" after installing a library
Module '"@angular/core"' has no exported member
These errors mean a library was compiled by a newer Angular than your app uses.
3 causes, each with a fixBack 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 fixBack end
TypeScript cannot find module '@nestjs/core' or its type declarations
error TS2307: Cannot find module '@nestjs/common' or its corresponding type declarations.
TS2307 means TypeScript searched node_modules and found neither the package nor its types.
3 causes, each with a fixBack 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