angular-cli different versions in the same compute

2020-02-17 07:48发布

问题:

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?

回答1:

You could use NVM which is a Node Version Manager and allows you to work with different global modules in each Node version.

In my case, I'm working with Node 6.9.5 and Angular2 in one project, and Node 10 and Angular6 in other.

You only have to pay attention which version is seated and work as normally as you do.



回答2:

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


回答3:

In case someone else came here like me with the same problem, I found this method as the easiest. I had @angular/cli@1.7.3 installed globally as I have an ongoing project created from that version(angular 5.0 roughly) but wanted to install @angular/cli@6.0.0. What I did was installing npx and then created the project using npx

Install npx frpm npm

npm install -g npx

Create new project with the desired cli version. Use @angular/cli@latest or just @angular/cli for the latest version.

npx -p @angular/cli@6.0.0 ng new my-project

Inside the project root folder, execute ng -v to see the version of you cli. But I recommend you use npx prefix to every command that uses ng as follows.

npx ng -v //or npx ng -version

npx ng generate component my-component

Here the npx look for the ng command exists locally in ./node_modules/.bin/ directory and executes it.



回答4:

Another solution is the following:

  1. Create a new folder and instantiate a node project with npm init.
  2. Install the Angular-CLI that related to the version that you need.
  3. Remove package.json file.
  4. Create/Import an Angular project that will have the version related to the CLI.

Further reading and explanation here



回答5:

create a Dockerfile

FROM node:8
WORKDIR /app
RUN npm install -g @angular/cli@6.1.1

build this docker image

docker build . -t angular/ng6.1.1

run the docker image build in the last step

docker run --rm -it -v $(pwd):/app angular/ng6.1.1 ng new hello-angular

You will find hello-angular project was created in your local folder, and if you want to use different angular version, change the angular/cli version in docker file and build a new docker image to create a new angular project.