Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular2: Cannot find module '@angular/core'

I followed the angular2 quick start to create my project and everything works fine but my "tsc -w" command line keeps on telling me:

app/components/company/company.ts(1,36): error TS2307: Cannot find module '@angular/core'.
app/components/company/company.ts(5,22): error TS2307: Cannot find module '@angular/router'.
app/components/mission/mission.ts(1,36): error TS2307: Cannot find module '@angular/core'.
app/components/mission/mission.ts(3,22): error TS2307: Cannot find module '@angular/router'.

And there is plenty of other line like this. However, everything is compiled successfully and my application works fine, but it's quite annoying to have all these warnings/errors as real errors something get lost in the middle of them.

I'm using Angular2 rc1, TypeScript 1.8.10, WebStorm EAP (altough I'm not using the WebStorm TypeScript compilation system, I'm relying on an open terminal with "tsc -w" command line).

I checked other question related to that on SO, but I didn't find anything that really helped me.

Update

Here is my tsconfig.json file:

{
  "compilerOptions": {
    "target": "es5",
    "sourceMap": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "module": "system",
    "noImplicitAny": false,
    "outDir": "js",
    "rootDir": "app"
  },
  "exclude": [
    "node_modules",
    "typings/main",
    "typings/main.d.ts"
  ]
}
like image 556
ssougnez Avatar asked May 27 '16 13:05

ssougnez


People also ask

How to resolve cannot find module error in angular?

Solution 1: Delete node_modules folder and run npm install But our local Angular application will have older versions of packages installed in node_modules folder. So delete the node_modules folder manually from the application directory. After that clear the cache using npm cache clean --verify .

Can't find module angular Devkit build angular?

To solve the error "Could not find module '@angular-devkit/build-angular'", make sure to install the package by opening your terminal in your project's root directory and running the following command: npm i -D @angular-devkit/build-angular and restart your IDE and development server.

Can not find module or its corresponding type declarations?

The "Cannot find module or its corresponding type declarations" error occurs when TypeScript cannot locate a third-party or local module in our project. To solve the error, make sure to install the module and try setting moduleResolution to node in your tsconfig. json file.


2 Answers

You first need to add typings at global level as below.

npm install -g typings

Then create typings.json using below command.

typings init

Paste below code in typings.json

{
  "name": "ng2-application",
  "globalDependencies": {
    "core-js": "registry:dt/core-js#0.0.0+20160725163759",
    "jasmine": "registry:dt/jasmine#2.2.0+20160621224255",
    "node": "registry:dt/node#6.0.0+20160909174046"
  }
}

Now you need to add below code in tsconfig.json

 "compilerOptions": {
        "moduleResolution": "node"
       }

After doing above steps you will able to get definitions on F12.

like image 151
Hardik Patel Avatar answered Oct 04 '22 17:10

Hardik Patel


You should use the moduleResolution attribute to node:

{
  "compilerOptions": {
    "target": "es5",
    "sourceMap": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "module": "system",
    "moduleResolution": "node", // <-----
    "noImplicitAny": false,
    "outDir": "js",
    "rootDir": "app"
  },
  "exclude": [
    "node_modules",
    "typings/main",
    "typings/main.d.ts"
  ]
}
like image 42
Thierry Templier Avatar answered Oct 04 '22 17:10

Thierry Templier