nvm realizes the coexistence of multiple versions of nodejs under windows

Table of Contents

1.Install nvm

1.1 Download the installation package

1.2 Install nvm

1.3 Configure nvm to download the image

1.4 Configure nvm environment variables

2. Use nvm

2.1 Basic commands

2.2 Use nvm to download nodejs

3.npm configure global dependencies and cache dependencies (optional)

4. Install commonly used modules

4.1 Install express

4.2 Install cnpm


I have been using node.js for front-end development recently. When installing packages and running existing projects, version conflicts always occur. I want to install multiple nodejs on my computer at the same time to achieve the coexistence of multiple versions of Node.js. , switch versions at any time for development, so I discovered nvm.

1.Install nvm

1.1 Download the installation package

Download address: nvm official website

1.2 Install nvm

After downloading, unzip the compressed package and you will get an exe file

Double-click the exe file, select [I accept the agreement], and click [Next]

Click [Browse], select the path, and click [next]

Select the location to control the nodejs version package

Click [Install]

1.3 Configure nvm download image

Configure the Taobao image. Its function is to use the Taobao image to download nodejs faster when using nvm.

In the directory where nvm is installed, find the [setting.txt] file

Double-click to open the txt file and enter the following content in it:

root: D:\
vm-nodejs\
vm # This is the directory where I installed nvm
path: G:\software\
odejs # This is the directory where nvm controls the current nodejs version package
Arch: 64
proxy: none
node_mirror: http://npm.taobao.org/mirrors/node/
npm_mirror: https://npm.taobao.org/mirrors/npm/

1.4 Configure nvm environment variables

In fact, when you install nvm, you have already configured the environment variables. Here, we take a look at how it is automatically configured.

It generates two environment variables in the system variables, namely [NVM_HOME] and [NVM_SYMLINK]. The path here is the path you selected during installation.

We also found that %NVM_HOME% and %NVM_SYMLINK% are automatically added to the Path.

[NVM_HOME] and [NVM_SYMLINK] will also be automatically added to the user variables.

And %NVM_HOME% and %NVM_SYMLINK% will be automatically added to its Path.

2. Use nvm

2.1 Basic Command

[win + r] Start the terminal command line.

View nvm version number:

nvm v

View all version numbers of nodejs downloaded by nvm:

nvm list
or
nvm ls

Show partial list of downloadable versions:

nvm list available

Show current version:

nvm current

Install the specified version of nodejs:

nvm install [nodejs version number] 32 //32 represents a 32-bit computer system, the default is 64-bit

Uninstall the specified version of nodejs:

nvm uninstall [nodejs version number]

Use the specified version of nodejs:

nvm use [nodejs version number]

2.2 Use nvm to download nodejs

1. Enter the official website of Node.js and view the existing historical versions

2. Use the installation command to install the version you need:

nvm install [nodejs version number]

3. View all nodejs currently managed by nvm

nvm list

Use the command to check the nodejs version:

node -v

If you find the following error, it’s because you haven’t used node yet.

4. Switch the nodejs you want to use

nvm use [version number of nodejs installed]

Then use node -v to view the current version

3.npm configure global dependencies and cache dependencies (optional)

Before configuring npm, I want to tell you that this step is optional configuration. I don’t think it is necessary to configure it, but I still wrote this chapter. Some people may have different opinions. needs.

Let me explain why I think there is no need to configure (just my personal opinion). In fact, after you configure nvm and download different versions of nodejs, your nvm will create two folders named after v + version number. . For example, if you download an express package under version 18.18.2, this package will only appear in the v18.18.2 folder, and will not appear in the v13.14.0 folder under another version 13.14.0. There is express package. Therefore, nvm has isolated the downloaded package.

1. Create two new folders, namely [node_global] and [node_cache]

The newly created path is placed where you set the nodejs version package earlier. In my case, it is under the G drive.

2. Configure address

After we download a nodejs, we need to configure the address through the following command.

Enable nvm command:

nvm on

Set your prefix and cache paths:

npm config set prefix “The path to node_global you just created”

npm config set prefix "G:\software\
odejs\
ode_global"

npm config set cache “The path where you just created node_cache”

npm config set cache "G:\software\
odejs\
ode_cache"

Tips:Each version needs to be configured again.

Configure globally dependent environment variables:

  • User variable Path settings

  • Add the NODE_PATH variable to the system variable, and add %NODE_PATH% to Path;

After adding the variable value, a [node_modules] folder will be automatically generated in [node_global]. If you don’t have one, you can create one yourself. There is no need to worry.

4. Install common modules

4.1 install express

After the configuration is complete, install a commonly used express module globally for testing.

[win + r] Start the terminal command and enter the installation command.

npm install express -g

After running the installation command, the package for you to install express will be in the [node_modules] folder of your current version.

4.2 Install cnpm

Before installing cnpm, you need to check the version number of npm and install the corresponding cnpm version. Otherwise, after you complete the installation, you will see the following error when you check the cnpm version:

View the commands of npm and cnpm:

npm -v
cnpm -v

Command to install cnpm:

npm install -g cnpm

If the installation fails, try Taobao mirror installation. Section 4.2 of this blog describes using mirror downloads.

Reference:

【1】Windows operating system installs multiple versions of nodejs-nodejs version control tool nvm

【2】How to elegantly let multiple versions of nodejs coexist?

【3】nvm installation and global dependency configuration (details)

[4] Some summaries on the use of node version management nvm (similar to nvm use is invalid, version switching causes npm failure, etc.