Cannot find name 'console' Error in TypeScript

avatar

Borislav Hadzhiev

Last updated: Feb 19, 2022

banner

Photo from Unsplash

Cannot find name 'console' Error in TypeScript #

To solve the "Cannot find name console" error, install the node types if running in Node.js by running npm i -D @types/node, or include dom in the lib array in your tsconfig.json file if your code is run in the browser.

cannot find name console

If your runtime is Node.js, make sure to install the typings for node, by opening your terminal in your project's root directory and running the following command:

shell
npm i -D @types/node

Your error should be resolved if your runtime is Node.js.

If it is not, make sure the types array in your tsconfig.json file contains "node".

tsconfig.json
{ "compilerOptions": { "types": [ // ... your other types "node" ], }, }

If your code is run in the browser or the solutions above did not resolve your error, add dom to the lib array in your tsconfig.json file.

tsconfig.json
{ "compilerOptions": { "lib": [ // ... your other libs "dom" ], // ... rest }, }

This should fix the error if running your code in the browser.

If the error is not resolved, try to delete your node_modules and package-lock.json files, re-run npm install and restart your IDE.

shell
rm -rf node_modules package-lock.json npm install

Make sure to restart your IDE if the error persists. VSCode glitches often and a reboot solves things sometimes.

Conclusion #

To solve the "Cannot find name console" error, install the node types if running in Node.js by running npm i -D @types/node, or include dom in the lib array in your tsconfig.json file if your code is run in the browser.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.