Cannot find module typescript: To resolve the cannot find module ‘typescript’ error, run the npm i -g typescript
command and build a symbolic link from the globally installed package to node_modules
using the npm link typescript
command.
Open a terminal in the root directory of your project and run the following commands:
Shell ( Command 1):
// It is use install typescript globally npm i -g typescript
Output:
added 1 package, and audited 2 packages in 4s found 0 vulnerabilities
Shell (Command 2):
// To Create an symbolic link from the global typescript package to the node modules of the current folder use below command npm link typescript
An unhandled exception occurred: cannot find module ‘typescript’: The error should be fixed when you run the above two commands.
If the global TypeScript installation fails, you may need to run the command with sudo
.
Run the following command to see if TypeScript has been successfully installed:
shell
tsc --version
The command’s output should show the version of the TypeScript package installed on your machine, for example, 4.8.0.
The npm link command builds a symbolic link from the globally installed package to the current folder’s
node_ modules/
directory.
If the error still persists, try deleting your node_modules
and package-lock.json
files, re-running npm install
, and restart your IDE.
shell:
// The below command is to remove the node_modules and package-lock.json rm -rf node_modules package-lock.json // It is use install typescript globally npm install // To Create an symbolic link from the global typescript package to the node modules of the current folder use below command npm link typescript
If the error still continues, be sure to restart your IDE. VSCode frequently crashes, and a reboot occasionally fixes the problem.
NOTE:
If this fails, try installing TypeScript locally.
shell:
// the below command is used to install the typescript and save it on the system npm install --save-dev typescript
This will add typescript to your project’s development dependencies, eliminating the need to run the link
command.