VSCode 找不到模块“@angular/core"或任何其他模块 [英] VSCode cannot find module '@angular/core' or any other modules
问题描述
我的项目是使用 Angular CLI 1.2.6 版生成的.
我可以编译项目并且它工作正常,但我总是在 VSCode 中收到错误消息:
<块引用>找不到模块'@angular/core'找不到模块@angular/router"找不到模块.....
我附上了我的 tsconfig.json
文件的内容这对我来说真的很令人沮丧,花了 2 个小时才弄清楚出了什么问题,我也卸载并重新安装了 VSCode它不起作用.
这是我的环境规范:
@angular/cli: 1.2.6节点:6.9.1操作系统:win32 x64@角度/动画:4.3.4@angular/common: 4.3.4@角度/编译器:4.3.4@角度/核心:4.3.4@角度/表格:4.3.4@角度/http:4.3.4@angular/平台浏览器:4.3.4@angular/platform-browser-dynamic: 4.3.4@角度/路由器:4.3.4@角度/cli:1.2.6@angular/compiler-cli: 4.3.4@角度/语言服务:4.3.4
操作系统:微软 vs 10 企业
项目根文件夹
.angular-cli.json.editorconfig.gitignore.vscode电子商务业力配置文件节点模块包.json量角器配置文件自述文件源文件配置文件tslint.json
node_modules
文件夹
-@angular--动画--cli- 常见的--编译器--compiler-cli- 核---@角度- -捆绑---core.d.ts---core.metadata.json---package.json---public_api.d.ts---自述文件---源代码---测试---testing.d.ts---testing.metadata.json- 形式--http--语言服务--平台浏览器--平台浏览器动态- 路由器--tsc 包装@ng-bootstrap@ngtools-@types--茉莉花--jasminewd2- 节点--q--selenium-webdriver
tsconfig.json
:
<代码>{compileOnSave":假,编译器选项":{outDir":./dist/out-tsc",sourceMap":真,声明":假,模块分辨率":节点",emitDecoratorMetadata":真,experimentalDecorators":真,目标":es5",类型根":[node_modules/@types"],库":[es2016",dom"]}}
我只在导入我自己创建的组件/服务时遇到这个问题对于像我这样接受的解决方案不起作用的人,可以试试这个:
添加
"baseUrl": "src"
在您的 tsconfig.json
中.原因是可视化代码 IDE 无法解析基本 url,因此无法解析导入组件的路径并给出错误/警告.
而 Angular 编译器默认使用 src
作为 baseurl,因此它能够编译.
注意:
您需要重新启动 VS Code IDE 才能使此更改生效.
如其中一条评论所述,在某些情况下,更改工作区版本也可能有效.更多详细信息:https://github.com/Microsoft/vscode/issues/34681#issuecomment-331306869
My project was generated with Angular CLI version 1.2.6.
I can compile the project and it works fine, but I always get error in VSCode telling me:
cannot find module '@angular/core' cannot find module '@angular/router' cannot find module .....
I have attached content of my tsconfig.json
file
this has been really frustrating for me, spending 2 hours to figure out what is wrong,
I have also uninstalled and reinstalled the VSCode
it doesn't work.
Here is my environment specification:
@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
OS: Microsoft vs 10 enterprise
project root folder
.angular-cli.json
.editorconfig
.gitignore
.vscode
e2e
karma.conf.js
node_modules
package.json
protractor.conf.js
README.md
src
tsconfig.json
tslint.json
node_modules
folder
-@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
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"
]
}
}
I was facing this issue only while importing my own created components/services For those of you like me, for whom the accepted solution did not work, can try this:
Add
"baseUrl": "src"
in your tsconfig.json
. The reason is that visual code IDE is unable to resolve the base url so is unable to resolve path to imported components and gives error/warning.
Whereas angular compiler takes src
as baseurl by default so it is able to compile.
NOTE:
You need to restart VS Code IDE to make this change come into effect.
EDIT:
As mentioned in one of the comments, in some cases changing workspace version might also work. More details here: https://github.com/Microsoft/vscode/issues/34681#issuecomment-331306869
这篇关于VSCode 找不到模块“@angular/core"或任何其他模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!