Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
344 views
in Technique[技术] by (71.8m points)

angular-cli different versions in the same computer

I'm working with Angular2 in several projects. Each of this projects uses a different version of angular-cli, so, I need to be able to run and compile for production each separately with the correct version of angular-cli. When I installed angular-cli locally using save-dev, ng commands not found so I cannot create a build distribution for the project.

So the question are, how can I have multiples angular-cli version in the same machine, without installed globally (with -g option)?, it is possible run ng commands without installing angular-cli globally?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should always be able to run local for your current directory version of ANGULAR CLI by running:

node_modules/.bin/ng <ANY NG COMMAND>

instead of just

ng <ANY NG COMMAND>

just make sure, you are running this command from the root directory where your main package.json is located (and its node_modules directory)

There is an npm command returning node_modules/.bin path (which may be used for shorter writing):

`npm bin`/ng <ANY NG COMMAND>

Note that back ticks are used around npm bin and not single quote sign.

ng command is also added to package.json scripts section, so it is also possible to run local copy like this:

npm run ng -- <NG_OPTIONS>

note -- flag terminator, it must be inserted before ng options.

If you want to create a new ng project using particular version but without installing @angular/cli globally, you can use npx command:

npx @angular/cli@7 new <APP_NAME>

here npx will temporary install @angular/cli with latest @7 version and run its ng executable with passed parameters.

You even can do something completely useless (because local copy of @angular/cli was installed with ng new). Run ng serve with @6 version of CLI on @7 new project:

cd <APP_NAME>
npx @angular/cli@6 serve

UPDATE

Running these commands will use ng executable located locally within node_modules/.bin/, so you do not need to have it installed globally:

cd <APP_NAME>
npx ng serve

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...