Cannot find type definition file for 'node' in TypeScript

avatar
Borislav Hadzhiev

Last updated: Feb 28, 2024
2 min

banner

# Cannot find type definition file for 'node' in TypeScript

To solve the error "Cannot find type definition file for node", install the node types by running npm i -D @types/node.

If the error persists, try adding node to your types array in tsconfig.json and restarting your IDE.

# Install the typings for Node.js

Open your terminal in the root directory of your project (where your package.json file is) and run the following command to install the typings for Node.

shell
# ๐Ÿ‘‡๏ธ with NPM npm install --save-dev @types/node # ๐Ÿ‘‡๏ธ with YARN yarn add @types/node --dev

install node typings

The code for this article is available on GitHub

If the error persists, try restarting your IDE.

# Add node to the types array in tsconfig.json

If that doesn't help, make sure the types array in your tsconfig.json file contains "node".

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

add node to types array in tsconfig json

That should fix the error in your project.

When the types option is specified, only the listed packages will be included in the global scope.

The example makes sure we include ./node_modules/@types/node, so we have typings for Node.js built-ins.

If types is not specified in your tsconfig.json file, all @types packages are included in your compilation - node_modules/@types/*.

# Delete your node_modules and reinstall your dependencies

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
# ๐Ÿ‘‡๏ธ (Windows) delete node_modules and package-lock.json rd /s /q "node_modules" del package-lock.json del -f yarn.lock # ๐Ÿ‘‡๏ธ (macOS/Linux) delete node_modules and package-lock.json rm -rf node_modules rm -f package-lock.json rm -f yarn.lock # ๐Ÿ‘‡๏ธ clean npm cache npm cache clean --force npm install

Restart your IDE and development server if the error persists.

Visual studio code often glitches and restarting the code editor sometimes helps.

The code for this article is available on GitHub
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.

Copyright ยฉ 2024 Borislav Hadzhiev