Solve - Cannot find module 'path' Error in TypeScript

avatar

Borislav Hadzhiev

Sat Feb 19 20221 min read

banner

Photo by Ben Curry

Solve - Cannot find module 'path' Error in TypeScript #

To solve the "Cannot find module path or its corresponding type declarations" error, install the types for node by running the command npm i -D @types/node. You can then import path with the following line of code import * as path from 'path'.

cannot find module path

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

Now you are able to import the path module with the following line of code.

index.ts
import * as path from 'path'; console.log(path);

If your error has not been resolved, open your tsconfig.json file and make sure the types array contains the string node.

tsconfig.json
{ "compilerOptions": { "types": [ "node" ] }, }
This should fix the error and now TypeScript should be able to find the type definitions for the path module.

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 still persists. VSCode glitches often and a reboot solves things sometimes.

Use the search field on my Home Page to filter through my more than 1,000 articles.