简体   繁体   中英

Use globally installed @angular/core instead of local

So I have @angular/core module installed as a global NPM dependency. I want all my Angular projects to use that version.

Unfortunately, it seems like Angular doesn't like it. Every time I run npm start in my project root, this is the error I got:

[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. So I've changed the baseUrl to the npm root -g directory, but then, another error has come up:

[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 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. Which is odd because ex @angular-devkit/core is also installed globally, so why on Earth the @angular/core is not recognized? When I install Angular locally then everything works fine.

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:

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

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