Among the version management tools for node
, nvm
is naturally well known, but we cannot forget n
from TJ
. These two, are the most mainstream solutions at the moment.
For more information on how to install and use these two tools, please see their respective homepages.
Next we focus on the operation and characteristics of nvm
and n
.
n
n
depends onnode
n is an
npm package
that needs to be installed globally.
|
|
This means that we need a node
environment before we can use n
to manage node
versions. We can either install a node
with Homebrew
, or download pkg
from the website, or we have to install a node
ourselves – n
itself can’t do that for you.
Then we can use n
to install different versions of node
. During installation, n
stores the specified version of node
and then copies it to the familiar path /usr/local/bin
, which is pretty straightforward. Of course, since n
will operate on non-user directories, you need to add sudo
to execute the command.
So it seems that n
is a very easy to understand solution in its implementation.
However, n
has the problem that the global module cannot be updated.
nvm
Let’s look at nvm
. Unlike n
, nvm
is not an npm package
, but a standalone package. This means we need to use its installation logic separately.
|
|
Or use Homebrew
to install it. After the installation, you need to change the shell
configuration (~/.zshrc or whatever), see the official documentation.
Then we can use nvm
to install different versions of node
.
During installation, nvm
stores the different versions of node
under ~/.nvm/<version>/
and then modifies $PATH
to include the path to the specified version of node
, so that the node
command we invoke will use the specified version of node
.
nvm
is obviously more complex thann
, but on the other hand, since it is a standalone package, the relationship between it and node looks more logical:nvm
does not depend on thenode
environment, it isnode
that depends onnvm
; unliken
, which creates a circular dependency-like problem.
How to choose?
Looking at it this way, the differences between nvm
and n
are still relatively large, as evidenced by.
- Ease of installation.
nvm
is obviously a lot of trouble to install;n
is more in line withnode
’s usual way of thinking. It’s a matter of opinion. - System support. Note that
nvm
does not supportWindows
. - Management of global modules.
n
does nothing with global modules, so there is a risk of global module execution errors after switchingnode
versions.
The global modules of nvm
exist in the sandbox of their respective versions and need to be reinstalled after switching versions, and there is no conflict between different versions.
About the node
path. n
is always /usr/local/bin;
nvm
requires a manually specified path.
So, how to choose?
It’s a matter of opinion, but here are some general suggestions.
- If you’re using
Windows
, there’s no choice but to usen
, or get aMac
. - If you switch
node
versions frequently (e.g., to test the latest version of a feature locally, while keeping your code compatible in production), you should only usenvm
for global module compatibility reasons. - If you’re a lightweight user who doesn’t need to worry about compatibility issues and is more concerned about the experience of installing and using
node
, then choosen
. - If you were to ask, which one did I end up with? I would say that I chose the more popular one.