How to fix Cannot find module 'typescript' in Angular 4?

Javascriptnode.jsAngular

Javascript Problem Overview


I generated Angular 4 app 3 weeks ago using the @angular/cli. After 2 weeks, I tried to run it with the command line ng serve but I am prompted an error below:

Cannot find module 'typescript'
Error: Cannot find module 'typescript'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (C:\Users\mypc\Documents\Angular Projects\my-angular-app\node_modules\@angular\cli\models\config\config.js:5:12)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)

Do you have any idea how to fix this? Thanks.

Javascript Solutions


Solution 1 - Javascript

For me just running the below command is not enough (though a valid first step):

npm install -g typescript

The following command is what you need (I think deleting node_modules works too, but the below command is quicker)

npm link typescript

Solution 2 - Javascript

This should do the trick,

npm install -g typescript

Solution 3 - Javascript

I was able to solve this problem by removing node_modules then running npm install

Solution 4 - Javascript

I had a similar problem when I rearranged the folder structure of a project. I tried all the hints given in this thread but none of them worked. After checking further I discovered that I forgot to copy an important hidden file over to the new directory. That was

> .angular-cli.json

from the root directory of the @angular/cli project. After I copied that file over all was running as expected.

Solution 5 - Javascript

If you don't have particular needs, I suggest to install Typescript locally.

NPM Installation Method
npm install --global typescript # Global installation
npm install --save-dev typescript # Local installation
Yarn Installation Method
yarn global add typescript # Global installation
yarn add --dev typescript # Local installation

Solution 6 - Javascript

Run: npm link typescript if you installed globally

But if you have not installed typescript try this command: npm install typescript

Solution 7 - Javascript

If you use yarn instead of npm, you can install typescript package for that workspace by running:

yarn add typescript

or you can install it globally by running:

sudo yarn global add typescript

to be available for any project.

Solution 8 - Javascript

I had the same problem. If you have installed first nodejs by apt and then you use the tar.gz from nodejs.org, you have to delete the folder located in /usr/lib/node_modules.

Solution 9 - Javascript

I had a very similar problem after moving a working project to a new subdirectory on my file system. It turned out I had failed to move the file named .angular-cli.json to the subfolder along with everything else. After noticing that and moving the file into the subdirectory, all was back to normal.

Solution 10 - Javascript

Run 'npm install' it will install all necessary pkg .

Solution 11 - Javascript

If you have cloned your project from git or somewhere then first, you should type npm install.

Solution 12 - Javascript

The best way is to do: npm install or npm i

this way all dependencies will be added.

Solution 13 - Javascript

I had this same problem using npm run start-dev as configured my package.json ("start-dev": "nodemon src/index.ts"). When i used nodemon src/index.ts in cmd work just fine, but to use npm run start-dev i had to use npm link typescript, it solve the problem.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionredshotView Question on Stackoverflow
Solution 1 - JavascriptDanAbdnView Answer on Stackoverflow
Solution 2 - JavascriptSajeetharanView Answer on Stackoverflow
Solution 3 - JavascriptredshotView Answer on Stackoverflow
Solution 4 - JavascriptTorsten BarthelView Answer on Stackoverflow
Solution 5 - JavascriptfreedevView Answer on Stackoverflow
Solution 6 - JavascriptRacalView Answer on Stackoverflow
Solution 7 - JavascriptAbdollahView Answer on Stackoverflow
Solution 8 - JavascriptAgustin LarreinegabeView Answer on Stackoverflow
Solution 9 - JavascriptvicatcuView Answer on Stackoverflow
Solution 10 - JavascriptSATISH RAJNALEView Answer on Stackoverflow
Solution 11 - JavascriptQodir AmirovView Answer on Stackoverflow
Solution 12 - JavascriptSarthak MaheshwariView Answer on Stackoverflow
Solution 13 - JavascriptSandro Antônio SouzaView Answer on Stackoverflow