Last updated: Feb 28, 2024
Reading timeยท2 min
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.
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.
# ๐๏ธ with NPM npm install --save-dev @types/node # ๐๏ธ with YARN yarn add @types/node --dev
If the error persists, try restarting your IDE.
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"
.
{ "compilerOptions": { "types": [ // ... your other types "node" ], }, }
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.
./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/*
.
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.
# ๐๏ธ (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.