Running multiple versions of the Angular CLI on one machine

In my various Angular2 projects, I am faced with the challenge of working with different versions of angular-cli. This means that in order to run and compile each project for production, I need to ensure that the correct version of angular-cli is being used. However, when I install angular-cli locally using save-dev, I encounter an issue where the ng commands are not recognized, making it impossible to create a build distribution for the project.

Therefore, my main question is how can I manage multiple versions of angular-cli on the same machine without installing them globally (using the -g option)? Is there a way to run ng commands without having to install angular-cli globally?

Answer №1

It is recommended to run the local version of ANGULAR CLI in your current directory by using the following command:

node_modules/.bin/ng <ANY NG COMMAND>

instead of just

ng <ANY NG COMMAND>

Make sure you run this command from the root directory where your main package.json is located (as well as its node_modules directory)

There is an npm command that gives you the path to node_modules/.bin, which can be used for shorter typing:

// does not work on Windows   
`npm bin`/ng <ANY NG COMMAND>

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

The ng command is also included in the scripts section of the package.json, so you can run the local copy like this:

npm run ng -- <NG_OPTIONS>

Remember to use the -- flag terminator before adding any ng options.

If you want to create a new Angular project using a specific version without globally installing @angular/cli, you can utilize the npx command:

npx @angular/cli@7 new <APP_NAME>

In this case, npx will temporarily install @angular/cli with the latest version (@7 at the time) and execute its ng command with the specified parameters.

You can even do something seemingly unnecessary (since a local copy of

@angular/cli</elode> was installed with <code>ng new
). Run ng serve with version 6 of CLI on a new project created with version 7:

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

UPDATE

Running these commands will use the locally stored ng executable within node_modules/.bin/, eliminating the need for global installation:

cd <APP_NAME>
npx ng serve

Answer №2

If you're facing a similar issue like me and searching for an easy solution, I stumbled upon this method that worked perfectly for me. Previously, I had globally installed

@angular/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c3a0afaa83f2edf4edf0">[email protected]</a>
as my project was based on that version (around angular 5.0), but now I needed to install
@angular/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7417181d34425a445a44">[email protected]</a>
.

The simple steps I followed were to install npx and then create the project using npx.

To install npx, run the following command:

npm install -g npx

To create a new project with the desired Angular CLI version, use either @angular/cli@latest or just @angular/cli:

npx -p @angular/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7f1c13163f49514f514f">[email protected]</a> ng new my-project

Within your project root folder, use ng --version to check the CLI version. It's recommended to prefix all commands involving ng with npx:

npx ng --version

npx ng generate component my-component

By utilizing npx, it searches for the ng command locally in the ./node_modules/.bin/ directory and executes it accordingly.

Answer №3

If you are looking to manage different versions of Node.js and work with global modules effortlessly, consider using NVM (Node Version Manager). This tool allows you to switch between different Node.js versions seamlessly.

For example, I am currently working on a project using Node 6.9.5 with Angular2, while on another project I use Node 10 with Angular6.

Simply keep track of the version you are working with and proceed as usual without any disruptions.

Answer №4

Begin by crafting a Dockerfile.

FROM node:8
WORKDIR /app
RUN npm install -g @angular/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="41222d2801776f706f70">[email protected]</a>

Proceed to build the docker image.

docker build . -t angular/ng6.1.1

Execute the docker image generated in the previous step.

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

After running, you will discover the creation of the hello-angular project within your local directory. Should you desire to utilize a different version of Angular, simply modify the angular/cli version in the docker file and initiate the building process again to construct a brand new Angular project.

Answer №5

Assumptions:

a. You currently have one version of Angular installed and functioning on your machine, but now you want to install a different version.

b. The Angular CLI has been globally installed for the current Angular project.

c. Your operating system is Ubuntu/Linux.

Some Important Commands:

$ node -v
$ npm -v
$ ng v
  1. Install NVM:

    i. Before installing nvm, execute this in your terminal: touch ~/.bash_profile

    Add the following lines to the  ~/.bashrc file
    
    sudo vim  ~/.bashrc
    
    source ~/.nvm/nvm.sh
    export NVM_DIR="$HOME/.nvm"
    

    ii. Next, run this command in your terminal:

    curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh | bash

    iii. Don't forget to restart your terminal or use the command $source ~/.nvm/nvm.sh (this will refresh the available commands in your system path).

    iv. In the terminal, check the installed version with the command $nvm --version

    v. Now, install a specific Node Version

    e.g. new/angular-repo/path$ nvm install 13.11.0

    vi. To see the list of installed node versions, use:

    new/angular-repo/path$ nvm list

    vii. To use a specific node version for the current project, use:

    new/angular-repo/path$ nvm use v13.11.0

  2. Install a specific Angular CLI

    new/angular-repo/path$ npm install @angular/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e98a8580a9dec7dbc7dd">[email protected]</a>

  3. To install all dependencies, run the following command:

    new/angular-repo/path$ npm install

    After completing the above command, ensure that Node, Angular CLI, NPM, and Angular are at the intended versions.

    new/angular-repo/path$ ng v

  4. If the npm version does not match your requirements in the above command, install a specific version using the following command

    new/angular-repo/path$ npm install npm@version

I hope this guide proves helpful.

Answer №6

Here is an alternative method:

  1. Begin by setting up a new folder and initializing a node project using npm init.
  2. Next, install the Angular-CLI version that aligns with your requirements.
  3. Delete the existing package.json file.
  4. Develop or import an Angular project with the appropriate version corresponding to the CLI.

For more detailed instructions and insights, click here

Answer №7

One effective approach is utilizing the npx command.

Here's a recommended solution:

  1. To get started, install npx by typing the following command in your terminal: npm install -g npx
  2. Next, create a new Angular project by executing this command:
    npx -p @angular/cli@VERSION_YOU_WANT_TO_USE ng new PROJECT_NAME

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Parent and router-outlet in Angular 4 fashion

Suppose we are presented with the following code: <div class="container"> <div class="row"> <div class="col-sm-4 col-md-3"> <app-sidebar></app-sidebar> </div> <div class="c ...

Send information to the child.component.ts file, not the child template

A scenario I am working on involves passing a value from a parent component to a child component. However, prior to displaying this value in the child.component.html file, I have a requirement to increment it by 2 within the app.component.ts file, and then ...

Problem Encountered in Angular 4 CLI when using enableProdMode in Internet Explorer 11

Currently, my Angular 4 application built using NodeJS works flawlessly on Internet Explorer 11 and the latest versions of Chrome and Firefox during development. However, when I enable Production Mode in the NodeJS Build as shown in the example from packa ...

Tips for successfully passing an Observable identifier to mergeMap

Monitoring the outputs of a list of observables with mergeMap is straightforward, as shown in this example code snippet: export class TestClass { test() { const observableA = of(1, 2, 3); const observableB = of(7, 3, 6); const observableC = ...

Tips for showcasing JSON data in an HTML table or list

I am seeking a way to dynamically display changing JSON object data in a table using Angular. The structure of the JSON object is subject to change due to server updates. JSON Object: { "This item has 1 value":1, "Another":30, "It ...

Encountering difficulties in setting up ng zorro using Angular CLI for installation

I'm facing an issue with installing ng-zorro. Every time I try to run the command ng add ng-zorro-antd, I encounter an error: npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! While resolving: <a href="/c ...

Live server code does not update with changes made in Angular

https://i.stack.imgur.com/Yo6Y8.png In the screenshot above, you can see my changes. I simply modified the text to read Download PDF, replacing what was there previously. Instead of the folder shown here, I used the code from this compiled file: https:// ...

Angular 6 tutorial: Creating a dynamic side navigation bar with swipe and drag functionality using Angular Material/Bootstrap

I am currently working on implementing a vertical swipeable/stretchable side nav-bar with angular-material in angular 6. However, I have encountered an issue with mouse interactions for stretching the nav-bar. Below is the code snippet: Here is the HTML c ...

Using Angular 4 Component to Invoke JavaScript/jQuery Code From an External File

I have written a jQuery code that is executed at ngAfterViewInit(). //myComponent.ts ngAfterViewInit() { $(function () { $('#myElement').click(function (e) { //the code works fine here }); } However, I want t ...

Is it necessary to unsubscribe from multiple switchmaps?

Consider the following scenario: Should I implement an unsubscribe in each instance of switchMap? And is it necessary to include a takeUntil after every switchMap operation? this.sharedSrv.postDetail.pipe( switchMap(post => { if (post) { th ...

Rotating images on a canvas

We're currently implementing Ionic and Angular in our project. One issue we are facing is regarding image rotation on canvas. When we click on an image, the rotation works perfectly if it's a jpg file. However, when we pass a base64 image, the r ...

Have there been any updates to ngFormControl in @angular/forms version 0.3.0?

I recently updated my Angular application from rc4 to rc5, along with upgrading angular forms from version 0.2.0 to 0.3.0. After the update, I started encountering an error that seems to be related to a change in ngFormControl within forms 0.3.0. zone.js: ...

What is the process for inserting a new element into an Array-type Observable?

I've been working on the Angular Tour of Heroes example and I have a feature where users can add new heroes to the existing list. Here's my add hero method in hero.service.ts: addNewHero(hero : Hero) : Observable<Hero> { console.log(her ...

Using an Angular HttpClient to authenticate with an API by utilizing a token for

I am currently attempting to fetch data from my Jenkins JSON API via HTTPClient. The data I need access to is restricted, so I must authenticate against Jenkins. To do this, I have generated an API Token. However, I am unsure of how to authenticate myself ...

Error encountered: JSON.parse failed due to unexpected input at position 281

I've been struggling to find a solution, as my searches always turn up irrelevant answers. I hope someone can help me out with this issue. Thank you in advance for your assistance. TypeError: JSON.parse Error: Unexpected token at position:281 imp ...

How to properly display an Angular Template expression in an Angular HTML Component Template without any issues?

When writing documentation within an Angular App, is there a way to prevent code from executing and instead display it as regular text? {{ date | date :'short'}} Many sources suggest using a span element to achieve this: <span class="pun"&g ...

IONIC 3 [ERROR] There was a problem encountered during the execution of cordova run android, resulting in an exit code of 1

While trying to run the command 'ionic cordova run android' for Ionic 3, I encountered an error that has left me stumped. Here's a snapshot of the error message: This Result Error [13:29:45] preprocess started ... [13:29:45] deeplinks sta ...

When using the router to send state, it returns as undefined

My scenario involves a component that utilizes the router to navigate to another component using a boolean variable in the URL: this.router.navigate(['/consume/course/' + this.id, {state: { isFirst }} ], { replaceUrl: true }); In the destination ...

Sign up for the category that failed to assign a worth

Currently, I have a service that includes a getter and setter: // Service import { Subject } from 'rxjs'; export class MyService { public currentUser: any = new Subject(); ... there is a function where I call setCurrent and assign value ...

Can Angular facilitate the establishment of an Express.js server directly in the browser?

Can an Express.js server be initialized within the browser using Angular? I am interested in executing Node scripts on the Express server via an Angular component. ...