83

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.

redshot
  • 2,930
  • 3
  • 31
  • 68

13 Answers13

146

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
GabLeRoux
  • 16,715
  • 16
  • 63
  • 81
DanAbdn
  • 7,151
  • 7
  • 27
  • 38
83

This should do the trick,

npm install -g typescript
GabLeRoux
  • 16,715
  • 16
  • 63
  • 81
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • 3
    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' – redshot Jun 18 '17 at 03:41
  • 1
    try to uninstall angular-cli globally, run npm cache clean and reinstall. – Sajeetharan Jun 18 '17 at 03:42
  • i have installed angular globally with "npm install -g @angular/cli" – redshot Jun 18 '17 at 03:43
  • uninstall it and do the fresh installation as above – Sajeetharan Jun 18 '17 at 03:44
  • 2
    This answer is doing the trick for me every time I don't have typescript installed globally. – skiabox Oct 16 '17 at 20:51
  • Only installing TypeScript locally with --save fixed the issue for me Found it here: https://www.bountysource.com/issues/32351321-uncaught-error-cannot-find-module-typescript – Satish P Feb 04 '19 at 03:22
  • But this is not really the solution. you should *link* the **global package** to the **local one** in `node_module` by runnig: `npm link typescript` – MoHo Oct 26 '19 at 10:28
48

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

redshot
  • 2,930
  • 3
  • 31
  • 68
  • 1
    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. – Tezza Nov 23 '18 at 13:18
  • Maybe instead to use `npm install` you can use `npm ci` – NsdHSO Jul 07 '22 at 21:40
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
Gedeon Mutshipayi
  • 2,871
  • 3
  • 21
  • 42
freedev
  • 25,946
  • 8
  • 108
  • 125
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.

Torsten Barthel
  • 3,059
  • 1
  • 26
  • 22
3

Run: npm link typescript if you installed globally

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

NsdHSO
  • 124
  • 7
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.

Abdollah
  • 4,579
  • 3
  • 29
  • 49
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.

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.

vicatcu
  • 5,407
  • 7
  • 41
  • 65
1

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

SATISH RAJNALE
  • 1,451
  • 1
  • 6
  • 8
1

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

  • 1
    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. – Jason Aller Jul 20 '20 at 15:37
0

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

this way all dependencies will be added.

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.