When working with Node.js, you would possibly encounter conditions the place you want to set up a number of variations of the runtime.
For instance, possibly you could have the newest model of Node arrange in your machine, but the venture you’re about to start out engaged on requires an older model. Or possibly you’re upgrading an outdated Node venture to a extra trendy model and it might be helpful to have the ability to change between the 2 when you make the transition.
With out a good software, this might imply spending loads of effort and time manually uninstalling and reinstalling Node variations and their international packages. Thankfully, there’s a greater manner!
Introducing nvm
nvm stands for Node Model Supervisor. Because the identify suggests, it helps you handle and change between totally different Node variations with ease. It supplies a command-line interface the place you possibly can set up totally different variations with a single command, set a default, change between them and rather 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 venture named nvm-windows that provides Home windows customers the choice of simply managing Node environments. Regardless of the identify, nvm-windows will not be a clone of nvm, neither is it affiliated with it. Nevertheless, the essential instructions listed beneath (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 current variations of Node.js
- delete any current Node.js set up directories (comparable to
C:Program Filesnodejs
) - delete the prevailing npm set up location (comparable to
C:Customers<consumer>AppDataRoamingnpm
)
After this, obtain and run the newest secure installer and you ought to be good to go!
macOS/Linux
In contrast to Home windows, eradicating earlier Node and npm installations in macOS and Linux is non-compulsory. If that is one thing you need to do, there are many good sources obtainable on-line. For instance, right here’s how one can take away Node on macOS and on Linux. And right here’s how one can take away any earlier npm set up you may 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
Be aware that the model quantity (v0.35.2
) will change because the venture develops, so it’s value checking the related part of venture’s residence web page to search out the newest model.
This can clone the nvm repository to ~/.nvm
and can make the required modifications to your bash profile, in order that nvm
is on the market 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 on the market anyplace in you terminal. Let’s see how one can use it to handle Node.js variations.
Set up A number of Variations of Node.js
One of the vital necessary 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 should 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 best obtainable 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 complete record of obtainable variations by working nvm ls-remote
.
For nvm-windows, that is nvm ls obtainable
.
Tip: itemizing all obtainable Node variations produces loads 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
.
npm
When putting in a Node.js occasion, nvm may also set up a suitable npm model. Every Node model would possibly convey a distinct npm model, and you’ll run npm -v
to test which one you’re at the moment utilizing. Globally put in npm packages aren’t shared amongst totally different Node.js variations, as this might trigger incompatibilities. Slightly, they’re put in alongside the present Node model in ~/.nvm/variations/node/<model>/lib/node_modules
. This has the added benefit that customers received’t require sudo
privileges to put in international packages.
Thankfully, when putting in a brand new Node.js model, you possibly can reinstall the npm international packages from a selected 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 corresponding npm model, and reinstall the worldwide npm packages you had put in for the 10.18.1
model.
If you happen to’re unsure what the newest model is, you should utilize the node
alias:
nvm set up node
This can at the moment pull in model 13.6.0
.
Or you possibly can set up the newest LTS launch, utilizing:
nvm set up --lts
This can at the moment pull in model 12.14.1
.
You may also uninstall any occasion you now not suppose is beneficial, by working:
nvm uninstall 13.6.0
Switching Between Variations
Up to now, we’ve seen how one can set up totally different Node variations. Now let’s undergo how one can change between them. Let me first observe that when a brand new model is put in, it’s robotically put to make use of. So in case you set up the newest Node.js model, and run node -v
proper after, you’ll see the newest model output.
To modify by way of put in variations, nvm supplies the nvm use
command. This works equally to the set up command. So, you 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 distinct model, nvm will make the node
occasion in your terminal symlink to the right Node.js occasion.
Customized Aliases
You may also 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, in case you now run:
nvm use awesome-version
nvm will change node
to model 13.6.0
. You may delete an alias by working:
nvm unalias awesome-version
You may also 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 Situations
At any time you possibly can test which variations you could have put in by working:
nvm ls
This can show one thing resembling the picture beneath.
The entry in inexperienced, with an arrow on the left, is the present model in use. Under the put in variations, there’s an inventory of obtainable aliases. Attempt executing the next now:
nvm use node
nvm ls
It can show like so:
You may also test 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 permit 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 form of potential with nvm in that, in case you create a .nvmrc
file inside a venture and specify a model quantity, you possibly can cd into the venture listing and kind nvm use
. nvm will then learn the contents of the .nvmrc
file and use no matter model of Node you specify.
If it’s necessary to you that this occurs robotically, there are a few snippets on the venture’s residence web page so that you can add to your .bashrc
or .zshrc
information to make this occur.
Right here’s the ZSH snippet. Place this beneath 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, while you change right into a listing with a .nvmrc
file, your shell will robotically change Node model.
Different nvm Instructions
nvm supplies a few different instructions which can be extra superior or much less generally used.
You may run a command immediately 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 selected model:
nvm exec 13.6.0 node --version
You will get the trail to the Node.js executable of a selected put in model:
nvm which 13.6.0
This could 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 software 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 observe 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 tasks. Your work is enormously appreciated by the Node.js neighborhood.
What about you? Do you employ nvm? How has your expertise been with it? Hit up Michael or James on Twitter in case you’d like to talk.
FAQs about Node Model Supervisor (NVM)
What’s NVM?
Node Model Supervisor (NVM) is a command-line software that permits you to simply handle a number of installations of Node.js and change between totally different variations on the identical machine.
Why would I want NVM?
NVM is beneficial while you work on tasks that require particular variations of Node.js. It permits you to change between these variations seamlessly, making certain compatibility and avoiding model conflicts.
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.
Which working methods are supported by NVM?
NVM helps Linux, macOS, and Home windows working methods.
How do I set up a selected model of Node.js utilizing NVM?
You may set up a selected 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.
How do I change between Node.js variations utilizing NVM?
To modify between Node.js variations, you should utilize the command nvm use [version]
. As an example, nvm use 12.22.6 will change to Node.js model 12.22.6.
Can I set a default Node.js model for use?
Sure, you possibly can set a default Node.js model utilizing the command nvm alias default [version]
. This model shall be used everytime you open a brand new terminal window.
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 ls
code> or nvm record
code>.
Can I uninstall a selected Node.js model with NVM?
Sure, you possibly can uninstall a selected Node.js model utilizing the command nvm uninstall [version]
code>. For instance, nvm uninstall 10.15.0 will uninstall Node.js model 10.15.0.
Does NVM solely handle Node.js variations?
No, NVM can even handle installations of npm (Node Package deal Supervisor) alongside Node.js variations. If you change Node.js variations, NVM may also change to the corresponding npm model.
Is NVM restricted to secure releases of Node.js?
No, NVM permits you to set up and handle secure, LTS (Lengthy-Time period Assist), and even growth variations of Node.js.
Can I take advantage of NVM with different Node.js model managers?
It’s usually really helpful to make use of just one Node.js model supervisor at a time to forestall conflicts. Utilizing NVM alongside different managers might result in surprising conduct.
How do I replace NVM itself?
You may replace NVM utilizing the command nvm replace.
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 tasks like “pyenv” for Python model administration which can be impressed by NVM’s idea.
Can NVM work with totally different shells?
Sure, NVM could be configured to work with varied shells like Bash, Zsh, and Fish. The set up directions usually embody particulars on configuring NVM on your most well-liked shell.