简体   繁体   中英

cannot find module '@angular/core' in Visual Studio Code IDE

Pretty simple code. But having the error cannot find module '@angular/core' .

course.component.ts

import {Component} from '@angular/core'


@Component({
    selector: 'courses'
})
export class CoursesComponent{

}

typings.json

 {
  "ambientDependencies": {
    "es6-shim": "github:DefinitelyTyped/DefinitelyTyped/es6-shim/es6-shim.d.ts#6697d6f7dadbf5773cb40ecda35a76027e0783b2"
 }
}

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "system",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
   "exclude": [
   "node_modules",
   "typings/main",
   "typings/main.d.ts"
  ]
}

package.json

{
  "name": "angular2-quickstart",
  "version": "1.0.0",
  "scripts": {
   "start": "concurrent \"npm run tsc:w\" \"npm run lite\" ",
   "tsc": "tsc",
   "tsc:w": "tsc -w",
   "lite": "lite-server",
   "typings": "typings",
   "postinstall": "typings install"
  },
   "license": "ISC",
   "dependencies": {
   "angular2": "2.0.0-beta.7",
   "bootstrap": "^3.3.6",
   "es6-promise": "^3.0.2",
   "es6-shim": "^0.33.3",
   "reflect-metadata": "0.1.2",
   "rxjs": "5.0.0-beta.2",
   "systemjs": "0.19.22",
   "zone.js": "0.5.15"
  },
 "devDependencies": {
  "concurrently": "^2.0.0",
  "lite-server": "^2.1.0",
   "typescript": "^1.7.5"
  }
}

I know this kind of question has been answered many times. But due to my ignorance, I still don't know.

Thanks for the help.

EDIT

My files are organized as捕获

For me it was due to the typescript module not selected correctly. So I selected same module value of "tsconfig.json File -> compilerOptions -> module: commonjs " at visual studio project properties. 在此处输入图片说明

Originally I downloaded the quick start seed from a course on udemy.com. Obviously the course was old. And something was mess up. I have to remind the instructor to correct it

Now I re-download the quick start from angular web site. The files structure is completely different from my original one. For example, it has a e2e folder and tslint.json . The package.json file is under thr root folder etc. tsconfig.json is in src folder.

A lot of changes, now it is working. Thanks everybody's hint.

It seems that your dependencies are using the package naming before RC versions.

This is explained here . In short, you need to add @angular/core (and the rest of the dependencies) in the package.json

EDIT: package.json

{
    "name": "angular2-quickstart",
    "version": "1.0.0",
    "scripts": {
        "start": "concurrent \"npm run tsc:w\" \"npm run lite\" ",
        "tsc": "tsc",
        "tsc:w": "tsc -w",
        "lite": "lite-server",
        "typings": "typings",
        "postinstall": "typings install"
    },
    "license": "ISC",
    "dependencies": {
        "@angular/common": "<your desired angular version>",
        "@angular/compiler": "<your desired angular version>",
        "@angular/core": "<your desired angular version>",
        "@angular/forms": "<your desired angular version>",
        "@angular/http": "<your desired angular version>",
        "@angular/platform-browser": "<your desired angular version>",
        "@angular/platform-browser-dynamic": "<your desired angular version>",
        "@angular/router": "<your desired angular version>",
        "bootstrap": "^3.3.6",
        "es6-promise": "^3.0.2",
        "es6-shim": "^0.33.3",
        "reflect-metadata": "0.1.2",
        "rxjs": "5.0.0-beta.2",
        "systemjs": "0.19.22",
        "zone.js": "0.5.15"
    },
    "devDependencies": {
        "concurrently": "^2.0.0",
        "lite-server": "^2.1.0",
        "typescript": "^1.7.5"
    }
}

On the Angular QuickStart package.json you have a clear picture of this example. Please, devote some time on the docs to make customizations according to your Angular version.

Hope this helps.

To me on Ionic 3 project, works just updating the typescript to the latest version

npm i --save-dev typescript@latest

Then, run:

npm update

If you have problems with ionic serve , just run:

npm install

To see more details on how to adjust VSCode IDE to "hide" this problem, see: https://code.visualstudio.com/docs/languages/typescript#_using-newer-typescript-versions

I had this same error but the solution was different for me, so I'll post here for anyone else that may have the same issue. I had cloned a repository from GitHub and I had this error and other similar errors on my import statements. I figured there were some files missing since there was a .gitignore. I ran the command npm install and it installed missing packages/files. It also resolved the error "No projects support the 'build' target" when trying to run npm build.

仅当不存在任何此类文件或文件夹时才会出现该问题因此,请查找名为“node_modules”的文件夹,该文件夹应存在于 package.json、tsconfig.json 所在的同一位置,然后按照此操作node_modules-> @angular -> 核心

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