Detailed explanation of nvm installation and use, with gnvm introduction

In my recent work, I encountered the need to deal with Node.jsmulti- . Since I developed it under the windows system, I used it nvmfor version management.
In fact, when I wrote an article introducing , I also mentioned nodemulti- version management tools, nvmand gnvmgave . And this article will introduce the use of and some basic principles of the two version management tools in nvmdetail again through more detailed content .gnvm

nvm

nvmIt is a tool for managing Node.jsmultiple versions. This article will introduce windowsthe usage in the environment.

nvm download and install

For the installation of windows system, there is a special nvm-windowsversion , which can be downloaded from the download link page.

For the original version of nvm, see: nvm .

insert image description here
Among them, noinstallis the installation-free version and setupthe 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

nvmDuring the installation process, you need to pay attention to the Node.jsset installation path, as shown in the following figure:

insert image description here
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.jsversion :

insert image description here
As shown in the picture above, the latest version is installed on my computer V18.14.1, select here , use to nvmcontrol 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 versionor nvm -v, nvm -vto display the version number information normally:

insert image description here
For example, what I installed locally nvmis 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, nvmmany commands are also provided.

  • nvm install <version>
    Use this command to install the specified version Node.js, which can be any version that has been released, just use the version number, such as installing 12.20.0:
    insert image description here

The version number can also be replaced by a specific description, latestindicating the latest version, ltsindicating the long-term support version, and the corresponding version can be installed normally.

  • nvm list
    After installing multiple versions, you can use nvm listthe command to view all nodethe versions installed in the current system. Abbreviation nvm ls:
    insert image description here
    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 different nodeversions and specify the version that the current system should use. It is relatively simple to use:
    insert image description here
    we switch to the latest installed version, and we can see that nodethe version has been switched smoothly.

Opening the command line tool as a system administrator is the correct operation.

  • nvm uninstall <version>
    Uninstall unwanted nodeversions

  • 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 .nodenodenvm ls availablenode
    --openssl-legacy-provider
    insert image description here

    --openssl-legacy-provider

nvm other commands

  • nvm current
    Check out nodethe version .

  • nvm root [path]
    Without is [path]used to view nvmthe installation directory of , all Node.jsare installed under this directory;
    with it [path], the directory where nodethe version .

  • nvm node_mirror [url]
    Set nodethe mirror url, which can be viewed in the filenvm under the installation directory .settings.txt

  • nvm npm_mirror [url]
    Set npmthe 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 rootchanging , 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.jsnvmnvm root
nodeNode.js
nvmV18.14.1nvm

insert image description here
In the original Node.jsinstallation directory, you can see that the folder has already displayed the icon of the connection (under win11 system):

insert image description here
Node.jsThe 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
nodenode

Global configuration for npm

In nvmmanagement , nodeafter 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 npminvolve some globally installed dependent packages, and there will be some differences.
If no global configuration is performed, npmthe environment is isolated. When installing a global dependent package, the related dependent package will be installed in nodethe directory of the current version, and only the global package installed under the current version npm list -gcan .
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 nodeshare the same npmglobal package installation by adding some configurations.
For example [email protected], node_modules\npmin the directory under the installation package of the version, there is a npmrcdefault configuration content: prefix=${APPDATA}\npm, which means that npmthe installed global dependencies will be installed AppData\Roaming\npmin the directory of the current user on the C drive.
If other versions nodedo 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]
npmAppData\Roaming\npm
.npmrc
insert image description here

PathNote that if you want the command to install dependent packages globally to work, you must add the specified AppData\Roaming\npmdirectory to the system environment variables .

gnvm

gnvmIt is also a very simple and easy-to-use Node.jsversion management tool, windowsand the installation in the environment is very simple.

gnvm download and install

gnvmIt 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:

insert image description here
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 Pathto .
  • If Node.js has already been installed, copy it directly to the installation directory where it Node.jsis located :

insert image description here

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 gnvmthe command
Open the command prompt box, enter the directory where the above gnvm.exefile 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:

insert image description here
.gnvmrcThe 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.jsAll version download directories
  • noderoot: the directory where the global Node.jsis located
  • globalversion: the currently used Node.jsversion
  • latestversion: the latest stable versionNode.js

gnvm uses

Common commands

gnvmMany useful commands are provided to manage Node.jsversions , 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, latestetc.
  • 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 gnvmof .
  • gnvm npm [version]: Used to download and install any version of npm.

gnvmIn the use of the command, there is a certain similarity with nvmis , and it is very simple and convenient, so I won't introduce it in detail here.

Fundamental

gnvmThe basic principle is to switch Node.jsversions .
When the user uses gnvm use [version]the command to switch Node.jsversions, gnvmit 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 Paththe environment variable, so that the command line terminal can find Node.jsthe exe file of this version.
At the same time, gnvmthis version will be set as the global default version.

other

gnvmAlthough 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.jsmanage multiple versions of , it is still the first choice in the windows environment nvm.

Guess you like

Origin blog.csdn.net/jimojianghu/article/details/130367440