简体   繁体   English

使用全局安装的 @angular/core 而不是本地

[英]Use globally installed @angular/core instead of local

So I have @angular/core module installed as a global NPM dependency.所以我将@angular/core模块安装为全局 NPM 依赖项。 I want all my Angular projects to use that version.我希望我所有的 Angular 项目都使用那个版本。

Unfortunately, it seems like Angular doesn't like it.不幸的是,Angular 似乎不喜欢它。 Every time I run npm start in my project root, this is the error I got:每次我在项目根目录中运行npm start时,都会出现以下错误:

[user@host]$ npm start
...
ERROR in Could not resolve module @angular/core

I've googled the error in many ways and found that maybe the tsconfig.json file is corrupted in some way.我以多种方式搜索错误,发现tsconfig.json文件可能以某种方式损坏。 So I've changed the baseUrl to the npm root -g directory, but then, another error has come up:因此,我将baseUrl更改为npm root -g目录,但随后出现了另一个错误:

[user@host]$ npm start
...
ERROR in No NgModule metadata found for 'AppModule'

What's even more interesting is that when I run ng version I can see strange Angular: undefined entry:更有趣的是,当我运行ng version我可以看到奇怪的Angular: undefined条目:

$ ng version

Angular CLI: 7.3.9
Node: 10.16.0
OS: linux x64
Angular: undefined
... 

Package                      Version
------------------------------------------------------
@angular-devkit/architect    0.13.9 (cli-only)
@angular-devkit/core         7.3.9 (cli-only)
@angular-devkit/schematics   7.3.9 (cli-only)
@schematics/angular          7.3.9 (cli-only)
@schematics/update           0.13.9 (cli-only)

So as you can see there's no @angular/core entry neither.正如你所看到的,也没有@angular/core条目。 Which is odd because ex @angular-devkit/core is also installed globally, so why on Earth the @angular/core is not recognized?这很奇怪,因为 ex @angular-devkit/core也是全局安装的,那么为什么地球上的@angular/core不被识别? When I install Angular locally then everything works fine.当我在本地安装 Angular 时,一切正常。

So I have solved it by "cheating".所以我通过“作弊”解决了它。 I have taken advantage of the fact that I use Linux OS and created a symlink to the global node_modules folder and now Angular thinks everything is in place:我利用了我使用 Linux 操作系统的事实,并创建了一个指向全局 node_modules 文件夹的符号链接,现在 Angular 认为一切都已就位:

$ ln -s /home/user/.nvm/versions/node/v10.15.3/lib/node_modules node_modules

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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