vs代码找不到模块'@angular/core'或任何其他模块

Ara*_*ash 66 visual-studio-code angular

我的项目是使用[Angular CLI] 1.2.6版生成的.

我可以编译项目,它工作正常,但我总是得到错误的vs代码告诉我找不到模块'@ angular/core'找不到模块'@ angular/router'找不到模块.....

截图 截图

我附上了我的tsconfig.json文件的内容,这对我来说真的很令人沮丧,花了2个小时来弄清楚出了什么问题,我还卸载并重新安装了vs代码它不起作用.

这是我的环境规范:

@angular/cli: 1.2.6
node: 6.9.1
os: win32 x64
@angular/animations: 4.3.4
@angular/common: 4.3.4
@angular/compiler: 4.3.4
@angular/core: 4.3.4
@angular/forms: 4.3.4
@angular/http: 4.3.4
@angular/platform-browser: 4.3.4
@angular/platform-browser-dynamic: 4.3.4
@angular/router: 4.3.4
@angular/cli: 1.2.6
@angular/compiler-cli: 4.3.4
@angular/language-service: 4.3.4
Run Code Online (Sandbox Code Playgroud)

os:微软vs 10企业

项目根文件夹

.angular-cli.json
.editorconfig
.gitignore
.vscode
e2e
karma.conf.js
node_modules
package.json
protractor.conf.js
README.md
src
tsconfig.json
tslint.json
Run Code Online (Sandbox Code Playgroud)

node_module文件夹

-@angular
--animations
--cli
--common
--compiler
--compiler-cli
--core
---@angular
---bundles
---core.d.ts
---core.metadata.json
---package.json
---public_api.d.ts
---README.md
---src
---testing
---testing.d.ts
---testing.metadata.json
--forms
--http
--language-service
--platform-browser
--platform-browser-dynamic
--router
--tsc-wrapped
@ng-bootstrap
@ngtools
-@types
--jasmine
--jasminewd2
--node
--q
--selenium-webdriver
Run Code Online (Sandbox Code Playgroud)

tsconfig.json:

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2016",
      "dom"
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

try*_*arn 92

我只是在导入我自己创建的组件/服务时才遇到这个问题对于像你这样的人,对于那些被接受的解决方案不起作用的人,可以试试这个:

"baseUrl": "src"
Run Code Online (Sandbox Code Playgroud)

在你的tsconfig.json.原因是可视代码IDE无法解析基本URL,因此无法解析导入组件的路径并提供错误/警告.

而角度编译器src默认采用baseurl,因此可以编译.

注意:

您需要重新启动VS Code IDE才能使此更改生效.

编辑:

正如其中一条评论中所述,在某些情况下,更改工作区版本也可能有效.更多细节在这里:https://github.com/Microsoft/vscode/issues/34681#issuecomment-331306869

  • 这对我有用......但现在得到另一个错误 Build: Class 'Subject<T>' 错误地扩展了基类 'Observable<T>' (2认同)
  • 这对我有用。我在tsconfig.app.json文件的basUrl中添加了src。“ baseUrl”:“ src”, (2认同)

小智 47

很可能在angular项目中缺少node_modules包,运行:

npm install
Run Code Online (Sandbox Code Playgroud)

在angular项目文件夹中.

  • 到底怎么回事!从 angular 开始,这拯救了我的今天 (3认同)
  • 然后重启VSCode (3认同)

小智 26

如果有任何更新或安装或清除缓存,则需要重新启动Visual代码


joe*_*oey 12

我的修复就是跑

npm install
Run Code Online (Sandbox Code Playgroud)

然后卸载,然后在visual studio中重新加载项目.


San*_*sai 9

在 VSCode 状态栏中,它必须显示打字稿版本 - 像这样

在此输入图像描述

单击该版本号将向您显示可用的不同版本。

在此输入图像描述

如果您使用的是 VSCode 版本,那么切换到 Workspace 版本可以解决问题(如果是 VScode 问题而不是您的tsconfig.json问题)(我已经在使用该版本,因此未突出显示)

在此输入图像描述


Kru*_*uti 7

从项目文件夹中删除节点模块文件夹。运行下面的命令

npm cache clean --force npm install

它应该工作。


小智 7

我遇到了同样的问题,这可能有两个原因-

  1. 您的src基本文件夹可能未声明,要解决此问题,请转到tsconfig.json并添加baseUrlsrc
{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "src",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "module": "esnext",
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "lib": [
      "es2018",
      "dom"
    ]
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true
  }
}
Run Code Online (Sandbox Code Playgroud)
  1. 您可能在 npm 中遇到问题,要解决此问题,请打开您的命令窗口并运行-npm install


Sib*_*enu 6

angular 5今天在我的应用程序中遇到了这个问题。而且对我有帮助的修复很简单。我加入"moduleResolution": "node"compilerOptions了在tsconfig.json文件中。我完整的tsconfig.json文件内容如下。

{
  "compileOnSave": false,  
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

moduleResolution指定模块的解决策略。此设置的值可以是nodeclassic。您可以在这里阅读更多有关此的内容。


小智 6

您需要手动安装它。

$ npm i @angular/core -s
Run Code Online (Sandbox Code Playgroud)


小智 6

我遇到了同样的问题,因为我正在尝试在 VS 代码中处理角度项目。

解决此问题的解决方案是。

  1. 如果项目文件夹中有 node_modules 文件夹,请删除该文件夹

2.在终端中运行以下命令

npm 安装

  1. 然后运行 ​​npmauditfix

  2. 然后运行 ​​npmauditfix --force

现在问题将得到解决。


小智 5

尝试使用:

npm audit fix --force
Run Code Online (Sandbox Code Playgroud)

进而:

npm install --save @ng-bootstrap/ng-bootstrap
Run Code Online (Sandbox Code Playgroud)

而不是@ng-bootstrap/ng-bootstrap全局保存。


小智 5

我有同样的问题。我通过清除位于“ C:\ Users \ Administrator \ AppData \ Roaming \ npm-cache”的 npm缓存来解决了该问题

或者您可以简单地运行:

npm cache clean --force
Run Code Online (Sandbox Code Playgroud)

然后关闭vscode,然后再次打开您的文件夹。


San*_*ron 5

我遇到了同样的问题,很奇怪,因为项目编译并运行时没有错误。我更新了 npm,然后重新安装了软件包

npm update
npm install
Run Code Online (Sandbox Code Playgroud)

那么 VS Code 别再说了。


Sem*_*van 5

我遇到过这个问题并通过以下命令修复了它。

npm cache clean --force
npm i @angular/core -s
Run Code Online (Sandbox Code Playgroud)

然后不要忘记重新启动VS code。它必须被修复。


Ara*_*ash 1

我卸载了已安装的所有扩展,结果发现来自以下地址的 JavaScript 和 TypeScript IntelliSense 扩展导致了问题。 https://marketplace.visualstudio.com/items?itemName=sourcegraph.javascript-typescript

这里的要点是,当您访问该网站时,您会看到有一个黄色标签,告诉您它处于预览版本中,但是当您在 vs 扩展中浏览时,您看不到该标签。

  • 另一种可能的解决方案是单击状态栏并选择“使用工作区版本”,请参阅https://github.com/Microsoft/vscode/issues/34681 (2认同)