In my recent work, I encountered the need to deal with Node.js
multi- . Since I developed it under the windows system, I used it nvm
for version management.
In fact, when I wrote an article introducing , I also mentioned node
multi- version management tools, nvm
and gnvm
gave . And this article will introduce the use of and some basic principles of the two version management tools in nvm
detail again through more detailed content .gnvm
nvm
nvm
It is a tool for managing Node.js
multiple versions. This article will introduce windows
the usage in the environment.
nvm download and install
For the installation of windows system, there is a special nvm-windows
version , which can be downloaded from the download link page.
For the original version of nvm, see: nvm .
Among them, noinstall
is the installation-free version and setup
the manual installation version.
Directly download the manual installation version nvm-setup
, the default installation is a little bit all the way, and the installation is complete.
Installation Precautions
nvm
During the installation process, you need to pay attention to the Node.js
set installation path, as shown in the following figure:
If a certain version is already installed on the computer Node.js
, the path selection here needs to be consistent with the path of this version, and in the subsequent installation process, choose to control this Node.js
version :
As shown in the picture above, the latest version is installed on my computer V18.14.1
, select here 是
, use to nvm
control the current version, and then the installation is complete.
To verify whether the installation is successful, reopen a command line (or git-bash), enter the command nvm version
or nvm -v
, nvm -v
to display the version number information normally:
For example, what I installed locally nvm
is the latest version, 1.1.11
.
how to use nvm
nvm command to manage node version
In addition to the commands for viewing the version mentioned above, nvm
many commands are also provided.
nvm install <version>
Use this command to install the specified versionNode.js
, which can be any version that has been released, just use the version number, such as installing12.20.0
:
The version number can also be replaced by a specific description,
latest
indicating the latest version,lts
indicating the long-term support version, and the corresponding version can be installed normally.
-
nvm list
After installing multiple versions, you can usenvm list
the command to view allnode
the versions installed in the current system. Abbreviationnvm ls
:
Among them, the description with asterisks and brackets indicates the version in use in the current system. -
nvm use [version]
This command is used to switch between differentnode
versions and specify the version that the current system should use. It is relatively simple to use:
we switch to the latest installed version, and we can see thatnode
the version has been switched smoothly.
Opening the command line tool as a system administrator is the correct operation.
-
nvm uninstall <version>
Uninstall unwantednode
versions -
Effectively supported version When
installing and using , we also need to pay attention to the current system 's support for the version, which can be checked by viewing the version that the system can install. If you install an unsupported version, you may encounter problems when using it. As shown below, I have installed a version 18+ on my computer to deal with the problem using a lower version of node, the following prompt appears: Not in An asterisk is displayed on a lower version, and the version of the current system cannot be used. Regarding the problem , you can check the blog post Problems Encountered in Nodejs Upgrade .node
node
nvm ls available
node
--openssl-legacy-provider
--openssl-legacy-provider
nvm other commands
-
nvm current
Check outnode
the version . -
nvm root [path]
Without is[path]
used to viewnvm
the installation directory of , allNode.js
are installed under this directory;
with it[path]
, the directory wherenode
the version . -
nvm node_mirror [url]
Setnode
the mirror url, which can be viewed in the filenvm
under the installation directory .settings.txt
-
nvm npm_mirror [url]
Setnpm
the mirror url, which can be viewed in the filenvm
under the installation directory .settings.txt
-
nvm proxy [url]
Without is[url]
used to view the proxy for downloading, with[url]
is used to set the proxy url. -
nvm on
Turn on version management. -
nvm off
Turn off version management.
Fundamentals of nvm
Without root
changing , when we use installnvm
, it will be installed in the root directory of , and the root directory can be viewed . The installation files of each installed version are in the root directory, and the version in use in the current system environment will be connected to the installation directory of . As shown below, the version selected to control when has been installed in the root directory of , as well as some other versions installed above:Node.js
nvm
nvm root
node
Node.js
nvm
V18.14.1
nvm
In the original Node.js
installation directory, you can see that the folder has already displayed the icon of the connection (under win11 system):
Node.js
The installation directory of this can be viewed in the filenvm
in the root directory . The installation directory that the version in the system environment depends on is linked to the real version through a soft link. Every time a version is switched, the new version is linked, so that the management between different versions is completed.settings.txt
node
node
Global configuration for npm
In nvm
management , node
after installing different versions of , there will be different versions npm
, such as [email protected]
corresponding to [email protected]
and [email protected]
corresponding to [email protected]
.
However, different versions npm
involve some globally installed dependent packages, and there will be some differences.
If no global configuration is performed, npm
the environment is isolated. When installing a global dependent package, the related dependent package will be installed in node
the directory of the current version, and only the global package installed under the current version npm list -g
can .
For example, if I install a global dependency package pm2
, I install one in [email protected]
the environment , [email protected]
and I also need to install one in the environment.
So how to share and install these global dependencies?
We can let multiple versions node
share the same npm
global package installation by adding some configurations.
For example [email protected]
, node_modules\npm
in the directory under the installation package of the version, there is a npmrc
default configuration content: prefix=${APPDATA}\npm
, which means that npm
the installed global dependencies will be installed AppData\Roaming\npm
in the directory of the current user on the C drive.
If other versions node
do not add this configuration file by default, the global dependency package will be installed in the node installation directory, such as and[email protected]
versions. Therefore, we can also add such configuration content in the corresponding version, so as to install different versions of global into the directory. Or, directly configure the file , and add the same content, which can also achieve the same effect:[email protected]
npm
AppData\Roaming\npm
.npmrc
Path
Note that if you want the command to install dependent packages globally to work, you must add the specifiedAppData\Roaming\npm
directory to the system environment variables .
gnvm
gnvm
It is also a very simple and easy-to-use Node.js
version management tool, windows
and the installation in the environment is very simple.
gnvm download and install
gnvm
It is written based on Go, and its installation file is a separate file, which does not depend on the system environment, and can be used after downloading.
Enter the download address and download the corresponding file directly. Here I choose 64-bit
:
After downloading to the computer, you get an exe file: gnvm.exe
. It
does not need to be installed, but directly copy the exe file to the corresponding directory:gnvm
- If you have never installed Node.js, you can copy it to any directory—but it is best to create a specific directory, and then add the current directory
Path
to . - If Node.js has already been installed, copy it directly to the installation directory where it
Node.js
is located :
gnvm initialization
Next, we also need to initialize through the command line tool.
Note: Please use the command prompt cmd of the win system here, and run it as an administrator, otherwise the initialization cannot be completed due to access rights issues, and gnvm
the command
Open the command prompt box, enter the directory where the above gnvm.exe
file is located, enter in the command line gnvm
, and complete the initialization. At this time, a configuration file will be automatically generated in the directory: .gnvmrc
, as shown in the following figure:
.gnvmrc
The content in the configuration is as follows:
registry: http://nodejs.org/dist/
noderoot: C:\Program Files\nodejs
globalversion: 18.14.1
latestversion: unknown
Definition Description:
- registry:
Node.js
All version download directories - noderoot: the directory where the global
Node.js
is located - globalversion: the currently used
Node.js
version - latestversion: the latest stable version
Node.js
gnvm uses
Common commands
gnvm
Many useful commands are provided to manage Node.js
versions , the following are the introductions of some commonly used commands:
- gnvm install [version]: used to install the specified version of Node.js, the version number can use such as
18.14.1
,latest
etc. - gnvm uninstall [version]: Used to uninstall the specified version of Node.js.
- gnvm use [version]: used to switch the currently used Node.js version.
- gnvm ls: Used to list all installed Node.js versions.
- gnvm node-version: Used to view the Node.js version currently in use.
- gnvm search [ . .*]: View online valid Node.js versions according to version rules.
- gnvm version: used to view the version information
gnvm
of . - gnvm npm [version]: Used to download and install any version of npm.
gnvm
In the use of the command, there is a certain similarity with nvm
is , and it is very simple and convenient, so I won't introduce it in detail here.
Fundamental
gnvm
The basic principle is to switch Node.js
versions .
When the user uses gnvm use [version]
the command to switch Node.js
versions, gnvm
it will check whether the corresponding version has been installed locally Node.js
. If this version exists, Gnvm will add the installation path of this version to Path
the environment variable, so that the command line terminal can find Node.js
the exe file of this version.
At the same time, gnvm
this version will be set as the global default version.
other
gnvm
Although it is very convenient to use, it has actually stopped maintenance and has not been updated for many years.
Therefore, if you want to Node.js
manage multiple versions of , it is still the first choice in the windows environment nvm
.