简体   繁体   中英

Angular 2 - Can't find module angular/core

I'm coming here and asking a question that has been already answered in another post, but all the solutions don't work. So, I'm creating a new angular2 app from the quickstart folder of angular website.

I've done npm install and all is okay, I have my node_modules folder at the root.

But, when I'm doing ng serve , this is the error I got :

ERROR in angular/src/app/app.component.ts (1,27): Cannot find module '@angular/core'.
ERROR in angular/src/app/app.component.ts (8,14): Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option to remove this warning.
ERROR in angular/src/app/app.module.ts (1,31): Cannot find module '@angular/platform-browser'.
ERROR in angular/src/app/app.module.ts (2,26): Cannot find module '@angular/core'.
ERROR in angular/src/app/app.module.ts (16,14): Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option to remove this warning.
ERROR in angular/src/main.ts (1,32): Cannot find module '@angular/core'.
ERROR in angular/src/main.ts (2,40): Cannot find module '@angular/platform-browser-dynamic'.

This is my folder structure :

在此处输入图片说明

And this is my tsconfig.json :

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [ "es2015", "dom" ],
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true,
    "typeRoots": [
      "../node_modules/@types/"
    ]
  },
  "compileOnSave": true,
  "exclude": [
    "node_modules/*",
    "**/*-aot.ts"
  ]
}

Edit :

This is my app.module.ts :

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

I went through my tsconfig.json file and found that your tsconfig.json differs from mine. The "include" & "exclude" key values pairs are different. Have a look.

tsconfig.json

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [
      "dom",
      "es2015"
    ],
    "module": "es2015",
    "moduleResolution": "node",
    "sourceMap": true,
    "target": "es5"
  },
  "include": [
    "src/**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ],
  "compileOnSave": false,
  "atom": {
    "rewriteTsconfig": false
  }
}

This is my tsconfig. maybe it helps:

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "baseUrl": "src",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2016",
      "dom"
    ]
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM