Solve - Cannot find module 'typescript' Error in TS

avatar

Borislav Hadzhiev

Sun Feb 20 20221 min read

banner

Photo by Jamie Street

Solve - Cannot find module 'typescript' Error in TS #

To solve the cannot find module 'typescript' error, make sure to install typescript globally by running the npm i -g typescript command and create a symbolic link from the globally-installed package to node_modules by running the npm link typescript command.

cannot find module typescript

Open your terminal in your project's root directory and run the following commands:

shell
# ✅ Install TypeScript globally npm install -g typescript # ✅ Create a symbolic link from the global package # to node_modules/ of current folder npm link typescript

Once you run the two commands, the error should be resolved.

If the global installation of TypeScript fails, you might have to run the command with sudo.

You can check if you have TypeScript installed successfully, by running the following command:

shell
tsc --version

The output of the command should show the version of the TypeScript package on your machine, e.g. 4.8.0.

The npm link command creates a symbolic link from the globally installed package to the node_modules/ directory of the current folder.

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 npm link typescript

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

If this also doesn't work, try installing TypeScript locally.

shell
npm install --save-dev typescript

This will add typescript to the development dependencies of your project, so you don't have to run the link command.

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