88

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.

9
  • 4
    did you install typescript globally? Commented Jun 18, 2017 at 3:35
  • Hi @Aravind I did not install typescript globally... Commented Jun 18, 2017 at 3:36
  • install it globally and set path in environmental variables. Commented Jun 18, 2017 at 3:36
  • I have installed it globally just now. Do you have a more specific guide to set path in environmental variables? Commented Jun 18, 2017 at 3:39
  • are you available in teamviewer? Commented Jun 18, 2017 at 3:46

13 Answers 13

154

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
Sign up to request clarification or add additional context in comments.

5 Comments

npm link typescript also worked for my nodejs project. thanks
If I could give you 14 upvotes I would. This did it for me. Everything broke after a node.js update.
anyone know why this doesnt happen during the install?
I installed that with "npm install -g typescript" and then used this command to update typescript "npm install -g npm". But, my problem has not been solved until now.
I already had TypeScript installed so I just did npm link typescript and it worked for me.
84

This should do the trick,

npm install -g typescript

7 Comments

Hi @Sajeetharan I have installed it globall using your command above. After running "ng serve" again , I am still prompted with: Cannot find module 'typescript' Error: Cannot find module 'typescript'
try to uninstall angular-cli globally, run npm cache clean and reinstall.
i have installed angular globally with "npm install -g @angular/cli"
uninstall it and do the fresh installation as above
This answer is doing the trick for me every time I don't have typescript installed globally.
|
50

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

2 Comments

Nice one. This worked for me when I had already tried the above solution. I was patching a local project with some project files from a tutorial that is slightly old. It kinda broke things, but deleting node_modules and running npm install fixed everything.
Maybe instead to use npm install you can use npm ci
11

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

Global installation

yarn global add typescript

Local installation

yarn add --dev typescript

Comments

9

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.

Comments

3

Run: npm link typescript if you installed globally

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

Comments

2

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.

Comments

1

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.

Comments

1

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.

Comments

1

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

1 Comment

fixed it for me
1

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

1 Comment

This is a duplicate of one of the nine existing answers on this three year old question. When adding an answer to an older question with an accepted answer it is very important to point out what new aspect of the question your answer addresses.
0

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

this way all dependencies will be added.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.