Installing a requiredf Node version Using nvm

When working with Node.js, you would possibly encounter conditions the place you might want to set up a number of variations of the runtime.

For instance, possibly you’ve got the newest model of Node arrange in your machine, but the mission you’re about to begin engaged on requires an older model, so you might want to downgrade. Or possibly you’re upgrading an previous Node mission to a extra fashionable model and it might be helpful to have the ability to swap between the 2 when you make the transition.

With out a good device, this is able to imply spending a variety of effort and time manually uninstalling and reinstalling Node variations and their world packages to put in the required Node model. Luckily, there’s a greater approach!

Introducing nvm

nvm stands for Node Model Supervisor. Because the identify suggests, it helps you handle and swap between totally different Node variations with ease. It supplies a command-line interface the place you may set up totally different variations with a single command, set a default, swap between them and far more.

OS Assist

nvm helps each Linux and macOS, however that’s to not say that Home windows customers should miss out. There’s a second mission named nvm-windows that gives Home windows customers the choice of simply managing Node environments. Regardless of the identify, nvm-windows is just not a clone of nvm, neither is it affiliated with it. Nonetheless, the fundamental instructions listed under (for putting in, itemizing and switching between variations) ought to work for each nvm and nvm-windows.

Set up

Let’s first cowl set up for Home windows, macOS and Linux.

Home windows

First, we have to do some preparation:

  • uninstall any present variations of Node
  • delete any present Node.js set up directories (comparable to C:Program Filesnodejs)
  • delete the present npm set up location (comparable to C:Customers<consumer>AppDataRoamingnpm)

After this, obtain and run the latest stable installer and try to be good to go!

macOS/Linux

In contrast to Home windows, eradicating earlier Node and npm variations in macOS and Linux is non-obligatory. If that is one thing you need to do, there are many good assets out there on-line. For instance, right here’s the best way to take away Node on macOS and on Linux. And right here’s how one can remove any previous npm installation you might need.

You may set up nvm utilizing cURL or Wget.

For cURL, run the next in your terminal:

curl -o- https://uncooked.githubusercontent.com/creationix/nvm/v0.35.2/set up.sh | bash

Or with Wget:

wget -qO- https://uncooked.githubusercontent.com/creationix/nvm/v0.35.2/set up.sh | bash

Notice that the model quantity (v0.35.2) will change because the mission develops, so it’s price checking the relevant section of project’s home page to seek out the latest model.

This may clone the nvm repository to ~/.nvm and can make the required adjustments to your bash profile, in order that nvm is offered from anyplace in your terminal.

And that’s it! Reload (or restart) your terminal and nvm is prepared for use.

Utilizing nvm

If put in accurately, the nvm command is offered anyplace in you terminal. Let’s see the best way to use it to handle Node.js variations.

Set up A number of Variations of Node.js

One of the essential components of nvm is, after all, putting in totally different variations of Node.js. For this, nvm supplies the nvm set up command. You may set up particular variations by working this command adopted by the model you need. For instance:

nvm set up 12.14.1

By working the above in a terminal, nvm will set up Node.js model 12.14.1.

Tip: nvm-windows customers must run nvm use 12.14.1 after putting in.

nvm follows SemVer, so if you wish to set up, for instance, the newest 12.14 patch, you are able to do it by working nvm set up 12.14.

nvm will then set up Node.js model 12.14.X, the place X is the very best out there model. On the time of writing, that is 1, so that you’ll have the 12.14.1 model put in in your system.

You may see the total record of obtainable variations by working nvm ls-remote.

For nvm-windows, that is nvm ls out there.

Tip: itemizing all out there Node variations produces a variety of output. Linux customers would possibly prefer to qpipe that to much less or grep the model they’re after. For instance, nvm ls-remote | much less, or nvm ls-remote | grep v12.

Set up npm

When putting in a Node.js occasion, nvm can even set up a appropriate npm model. Every Node model would possibly deliver a special npm set up, and you’ll run npm -v to examine which one you’re at present utilizing. Globally put in npm packages aren’t shared amongst totally different Node.js variations, as this might trigger incompatibilities. Fairly, they’re put in alongside the present Node model in ~/.nvm/variations/node/<model>/lib/node_modules. This has the added benefit that customers gained’t require sudo privileges to put in world packages.

Luckily, when putting in a brand new Node.js model, you may reinstall the npm world packages from a particular model. For instance:

nvm set up v12.14.1 --reinstall-packages-from=10.18.1

By working the above, nvm will set up Node.js model 12.14.1, the precise model of npm required, and reinstall the worldwide npm packages you had put in for the ten.18.1 model.

If you happen to’re undecided what the newest model is, you should utilize the node alias:

nvm set up node

Or you may set up the latest LTS release, utilizing:

nvm set up --lts

It’s also possible to uninstall any occasion you now not assume is beneficial, by working:

nvm uninstall 13.6.0

Switching Between Variations

To date, we’ve seen the best way to set up totally different Node variations. Now let’s undergo the best way to swap between them (successfully upgarding or downgrading your Node model). Let me first word that when a brand new model is put in, it’s robotically put to make use of. So should you set up the newest Node.js model, and run node -v proper after, you’ll see the newest model output.

To modify by means of put in variations, nvm supplies the nvm use command. This works equally to the set up command. So, you might want to comply with this by a model quantity or an alias.

Change to Node.js model 13.6.0:

nvm use 13.6.0

Change to Node.js model 12.14.1:

nvm use 12.14.1

Change to the newest Node.js model:

nvm use node

Change to the newest LTS model:

nvm use --lts

When switching to a special model, nvm will make the node occasion in your terminal symlink to the right Node.js occasion.

Customized Aliases

It’s also possible to create customized aliases past those that include nvm. For instance, by working:

nvm alias awesome-version 13.6.0

You’re setting an alias with the identify β€œawesome-version” for Node.js model 13.6.0. So, should you now run:

nvm use awesome-version

nvm will swap node to model 13.6.0. You may delete an alias by working:

nvm unalias awesome-version

It’s also possible to set a default occasion for use in any shell, by concentrating on a model to the β€œdefault” alias, like so:

nvm alias default 12.14.1

Itemizing Put in Cases

At any time you may examine which variations you’ve got put in by working:

nvm ls

This may show one thing resembling the picture under.

nvm versions list

The entry in inexperienced, with an arrow on the left, is the present model in use. Beneath the put in variations, there’s an inventory of obtainable aliases. Strive executing the next now:

nvm use node
nvm ls

It can show like so:

nvm use and versions list

It’s also possible to examine what’s the present model in use with the command:

nvm present

Specify a Node Model on a Per-project Foundation

Model managers comparable to rbenv assist you to specify a Ruby model on a per-project foundation (by writing that model to a .ruby-version file in your present listing). That is type of doable with nvm in that, should you create a .nvmrc file inside a mission with a particular Node model, you may cd into the mission listing and sort nvm use. nvm will then learn the contents of the .nvmrc file and use no matter model of Node you specify.

If it’s essential to you that this occurs robotically, there are a few snippets on the mission’s residence web page so that you can add to your .bashrc or .zshrc recordsdata to make this occur.

Right here’s the ZSH snippet. Place this under your nvm config:

autoload -U add-zsh-hook
load-nvmrc() {
  native node_version="$(nvm model)"
  native nvmrc_path="$(nvm_find_nvmrc)"

  if [ -n "$nvmrc_path" ]; then
    native nvmrc_node_version=$(nvm model "$(cat "${nvmrc_path}")")

    if [ "$nvmrc_node_version" = "N/A" ]; then
      nvm set up
    elif [ "$nvmrc_node_version" != "$node_version" ]; then
      nvm use
    fi
  elif [ "$node_version" != "$(nvm version default)" ]; then
    echo "Reverting to nvm default model"
    nvm use default
  fi
}
add-zsh-hook chpwd load-nvmrc
load-nvmrc

Now, whenever you change right into a listing with a .nvmrc file, your shell will robotically change Node model.

Automatically apply nvm use

Different nvm Instructions

nvm supplies a few different instructions which are extra superior or much less generally used.

You may run a command straight for an put in model with out switching the node variable:

nvm run 13.6.0 --version

You may run a command on a sub-shell, concentrating on a particular model:

nvm exec 13.6.0 node --version

You will get the trail to the Node.js executable of a particular put in model:

nvm which 13.6.0

This may be helpful when configuring a textual content editor plugin that should know the place your present model of Node lives.

Conclusion

nvm is a superb device for any Node.js developer. It allows a concern-free set up and simple switching between totally different variations, saving time for what actually issues.

A thanks word to Tim Caswell, the creator of nvm, and in addition to Corey Butler for the nvm for Home windows help, and naturally to these contributing to those nice initiatives. Your work is drastically appreciated by the Node.js group.

What about you? Do you utilize nvm? How has your expertise been with it? Hit up Michael or James on Twitter should you’d like to talk.

FAQs about Node Model Supervisor (NVM)

Which working methods are supported by NVM?

nvm helps each Linux and macOS. Home windows customers ought to use both WSL, orΒ nvm-windows.

How do I set up NVM?

You may set up NVM by following the set up directions offered on your working system. Sometimes, it includes working a script in your terminal or command immediate.

Can NVM work with totally different shells?

Sure, NVM might be configured to work with varied shells like Bash, Zsh, and Fish. The set up directions typically embody particulars on configuring NVM on your most popular shell.

Is NVM restricted to Node.js, or can it handle different runtime variations too?

Whereas NVM’s major focus is on Node.js, there are community-maintained initiatives like β€œpyenv” for Python model administration which are impressed by NVM’s idea.

How do I replace NVM itself?

You may replace NVM utilizing the command nvm replace.

Can I exploit NVM with different Node.js model managers?

It’s typically really useful to make use of just one Node.js model supervisor at a time to stop conflicts. Utilizing NVM alongside different managers may result in surprising conduct.

Is NVM restricted to steady releases of Node.js?

No, NVM means that you can set up and handle steady, LTS (Lengthy-Time period Assist), and even improvement variations of Node.js.

Does NVM solely handle Node.js variations?

No, NVM may also handle installations of npm (Node Bundle Supervisor) alongside Node.js variations. While you swap Node.js variations, NVM can even swap to the corresponding npm model.

How do I set up a particular model of Node.js utilizing NVM?

You may set up a particular model of Node.js utilizing the command nvm set up [version]. For instance, nvm set up 14.17.3 will set up Node.js model 14.17.3.

Can I uninstall a particular Node.js model with NVM?

Sure, you may uninstall a particular Node.js model utilizing the command nvm uninstall [version]. For instance, nvm uninstall 10.15.0 will uninstall Node.js model 10.15.0.

How do I record the Node.js variations I’ve put in with NVM?

You may record the put in Node.js variations utilizing the command nvm lscode or nvm record.

Can I set a default Node.js model for use?

Sure, you may set a default Node.js model utilizing the command nvm alias default [version]. This model will probably be used everytime you open a brand new terminal window.

How do I swap between Node.js variations utilizing NVM?

To modify between Node.js variations, you should utilize the command nvm use [version]. As an illustration, nvm use 12.22.6 will swap to Node.js model 12.22.6.

Why would I want NVM?

NVM is beneficial whenever you work on initiatives that require particular variations of Node.js. It means that you can swap between these variations seamlessly, making certain compatibility and avoiding model conflicts.

What’s NVM?

Node Model Supervisor (NVM) is a command-line device that means that you can simply handle a number of installations of Node.js and swap between totally different variations on the identical machine.

Can I downgrade to a earlier Node model with NVM?

Sure, you may. You may set up a earlier model of Node.js utilizing the command nvm set up [version].To modify between Node.js variations, you should utilize the command nvm use [version]. As an illustration, nvm use 12.22.6 will swap to Node.js model 12.22.6.