DEV Community

Cover image for How to run multiple Node and Angular versions simultaneously

How to run multiple Node and Angular versions simultaneously

Patrícia Pereira on March 20, 2020

In my line of work as a Web Engineer, I have leaped through a decent amount of projects in a not so big amount of time. Not only that, but all of t...
Collapse
 
andyghiuta profile image
Andy G

There's also nsv which I found to work better in Windows, especially for upgrading npm.

Collapse
 
patricepeartree profile image
Patrícia Pereira

I have heard of it, but I have never used it, I'll be sure to take a look at it someday.
What's the difference between them for that specific case of upgrading npm?

Collapse
 
andyghiuta profile image
Andy G

With nvm I had to use npm-windows-upgrade package, while with nvs you can simply do npm i -g npm. I also had trouble with the antivirus (McAfee) which had to be turned off while installing another version of node with nvm.

Thread Thread
 
patricepeartree profile image
Patrícia Pereira

Interesting to know... I've just used nvm on a Linux environment, never got to try nvm-windows, but now I'll keep that in mind.

Collapse
 
brightknight08 profile image
Tim Anthony Manuel

Thanks for the heads up!

Collapse
 
weasnerb profile image
Brian Weasner

Instead of creating scripts in every project of yours to run the local version of the angular cli, you can use the npx package! You can install it globally and then whenever you are inside a project with node modules you can run "npx the-tool-name" and it will run the tool local to the project. (In this instance if "the-tool-name" was ng, it would run the local version of the angular cli without the need to add ng as a script in the package.json)

Collapse
 
patricepeartree profile image
Patrícia Pereira

npx is really an useful tool, thanks for the tip!

Collapse
 
iranicus profile image
iranicus • Edited

Hey Patricia, just came across this article now and know the pain of operating across multiple angular version and node projects so this is very handy.

I set it up on my linux for my own personal projects and it seems after different node versions via nvm are installed and I run the "nvm run npm" start in the angular project directory I got a Cannot find module npm issue. I raised a ticket on the nvm github and yesterday I was told that after running "nvm use node version" its not necessary to run "nvm run npm start" due to the prior "nvm use" command being used.

Not sure if things have changed since you first uploaded this or not but wanted to pass that onto folks :>
If you are interested in the details of it the ticket can be found on: github.com/nvm-sh/nvm/issues/2284

Thanks now

Collapse
 
patricepeartree profile image
Patrícia Pereira

Hi. Yes, you're right, after nvm use you can execute only npm start and it will use the Node version you selected.

But actually the correct command to run npm as you were trying would be nvm exec npm start (not nvm run), maybe that's why you were getting an error? I see I have that command wrong in one of the sections of the post, I have a nvm run where a nvm exec should be (I'm going to edit that). Running the commands like this is only usefull if you have a .nvmrc file to avoid having to execute nvm use every time. But like you said, if you run nvm use, then you can actually just use the normal node and npm commands!

Collapse
 
patryktech profile image
Patryk

I just use docker. Define an image version as a variable, and you can easily manage all your infrastructure as code.

Collapse
 
patricepeartree profile image
Patrícia Pereira

Hi Patryk! Yeah, that was also my original approach (and the one I actually keep using the most). Do you use mounted volumes in your containers? I do, and sometimes the file system access to the host makes read/write operations slow, and that was what made me look for a different approach.

Collapse
 
patryktech profile image
Patryk

I use both bind mounts for development (in my Python and node containers), and named volumes in dev and production, e.g. for my databases.

I use Linux for development and production, and I've never noticed any performance issues.

Thread Thread
 
patricepeartree profile image
Patrícia Pereira

Same here, regarding the volumes. But when in Windows with a bind mount for development, a simple npm install would take a lot longer than when run in the host. Probably Windows related...

Collapse
 
clementmwendwa profile image
Clement Mwendwa

Why use nvm execute when you could just use npm? Considering you've switched the node versions already.

Collapse
 
patricepeartree profile image
Patrícia Pereira • Edited

By "switched the node versions already" do you mean executed nvm use [version]? Because if so, then when you open a new terminal, the version nvm will be using will no longer be the one you just defined. It will be the one to which the nvm default alias is pointing to.

The combination .nvmrc file + nvm exec/nvm run avoids having to run nvm use every time.

Collapse
 
clementmwendwa profile image
Clement Mwendwa

Got you, yeah meant after nvm use. For what it's worth though there's a tool to automatically switch versions when you have a .npmrc file
github.com/wbyoung/avn, you could have a script to do that as well.

Collapse
 
chachan profile image
Cherny

a bit offtopic but since this is about Angular versions, is there a way to know which versions of TypeScript are supported in the current Angular version?

Collapse
 
patricepeartree profile image
Patrícia Pereira

You can check the supported TypeScript versions for an Angular version in the @angular/compiler-cli/package.json:

{
  ...
  "peerDependencies": {
    ...
    "typescript": ">=3.6 <3.7"
  }
}

There seems to be a way to disable this version checking in case you want to install a version outside of that range. But I guess you should do that at your own risk.

Collapse
 
chachan profile image
Cherny

oh that's helpful. Thanks @patricepeartree

Collapse
 
nuni233 profile image
nuni233

How does nvm will affect the internal operations of Visual Code to create components, services etc. ?

Collapse
 
avimoraly profile image
Avi Moraly

what about when you want to create new angular project using
"ng new my_new_project"
what you gonna do when you still don't have local directory?

Collapse
 
patricepeartree profile image
Patrícia Pereira

Well, I guess you can have some workarounds for that. You can npm init a directory just to be able to install locally the Angular version you want, and then use that one to ng new your new project. Or you can leverage Docker with mounted volumes and create a project from within a container with your desired Angular version.