Detailed operation of multi-version node installation and switching

Reasons for installing multiple versions of node:

During the project development process, different projects use different versions of node, sometimes because the version of node is too high or too low, resulting in an error; how to install multiple versions of node in the same system, here are two ways, go down Let's choose the way that suits you

Method 1: Use nvm for management

Introduction to NVM

The full name of NVM is Node Version Manager, which is a tool for managing NodeJS versions.
By default, NVM only supports Linux and OS X, and does not support Windows. There are 2 alternatives for Windows operating systems:

  1. nvm-windows
  2. nodist
    here only introduces the method of using nvm-windows to install and switch multiple versions of NodeJS.

Notes before installation:

  • If there is a node environment before, it needs to be uninstalled cleanly
  • If after installing node, the input nvm use <版本号>is still invalid (the node version is not selected before "*"), you need to reinstall
  • Do not have Chinese characters or spaces in the installation path, otherwise an error will occur

insert image description here

Uninstall the installed nodeJS

  1. Click the start menu, find the folder of nodejs, click the arrow to uninstall (note: before doing this, you need to end the project using nodejs)
    insert image description here
  2. Detect whether nodejs still exists in the system
    (1) View it in the start menu
    (2) Enter in the cmd window node -vto view it

Installation and use of nvm

  1. nvm download

Installation package download address: https://github.com/coreybutler/nvm-windows/releases
Note: installation environment win10 system, 64-bit
Note : installation environment win10system , 64bit

  1. nvm installation (Note: It is best not to install it on the C drive)
    (1) Create two new folders under the specified file, one is used to store the nvm installation file, and the other is used to store the node file
    insert image description here
    (2) will be downloaded Put the installation package into the nvm folder, decompress it, and install it.
    Note: When installing, select the newly created folder as the specified installation directory
    insert image description here
    (3) Select the nvm installation path, click Next
    insert image description here
    (4) Select the nodejs path
    insert image description here
    (5) Click Install—>Click Finish to complete the installation.
    (6) Confirm whether the installation is successful Open
    CMD, enter the command nvmcommand , the installation is successful, the following display settings.txt file, add the following code at the end (do not change, downloading node may report an error)
    insert image description here



    insert image description here
arch: 64
proxy:
node_mirror: https://npm.taobao.org/mirrors/node/
npm_mirror: https://npm.taobao.org/mirrors/npm/

Note:

arch: Windows operating system digit
proxy: proxy, Taobao NodeJS image and NPM image

insert image description here
Note:

root: NVM installation path
path: NodeJS shortcut path

Installation and switching of different versions of node

  1. After NVM is successfully installed, win + r and enter cmd to open a new cmd window, and you can view all available commands through nvm -vthe command .
C:\>nvm -v

Running version 1.1.7.

Usage:

  nvm arch                     : Show if node is running in 32 or 64 bit mode.
  nvm install <version> [arch] : The version can be a node.js version or "latest" for the latest stable version.
                                 Optionally specify whether to install the 32 or 64 bit version (defaults to system arch).
                                 Set [arch] to "all" to install 32 AND 64 bit versions.
                                 Add --insecure to the end of this command to bypass SSL validation of the remote download server.
  nvm list [available]         : List the node.js installations. Type "available" at the end to see what can be installed. Aliased as ls.
  nvm on                       : Enable node.js version management.
  nvm off                      : Disable node.js version management.
  nvm proxy [url]              : Set a proxy to use for downloads. Leave [url] blank to see the current proxy.
                                 Set [url] to "none" to remove the proxy.
  nvm node_mirror [url]        : Set the node mirror. Defaults to https://nodejs.org/dist/. Leave [url] blank to use default url.
  nvm npm_mirror [url]         : Set the npm mirror. Defaults to https://github.com/npm/cli/archive/. Leave [url] blank to default url.
  nvm uninstall <version>      : The version must be a specific version.
  nvm use [version] [arch]     : Switch to use the specified version. Optionally specify 32/64bit architecture.
                                 nvm use <arch> will continue using the selected version, but switch to 32/64 bit mode.
  nvm root [path]              : Set the directory where nvm should store different versions of node.js.
                                 If <path> is not set, the current root will be displayed.
  nvm version                  : Displays the current running version of nvm for Windows. Aliased as v.
  1. Use nvm install <版本号>the command to install the specified version of NodeJS
C:\>nvm install v8.16.2
Downloading node.js version 8.16.2 (64-bit)...
Complete
Downloading npm version 6.4.1... Complete
Installing npm v6.4.1...

Installation complete. If you want to use this version, type

nvm use 8.16.2

Note : If it only Downloading node.js version 8.16.2 (64-bit)...continues as follows, it should be that npm has not been installed successfully. You can find the nvm directory by setting the Taobao mirror as above. Add two lines of configuration to the settings.txt file and walk through the Taobao mirror to solve it successfully.

C:\>nvm install v8.16.2
Downloading node.js version 8.16.2 (64-bit)...

  1. After the installation is successful, a v8.16.2 folder appears under the NVM installation directory. At this time, you can try to use the above nvm listcommand to view the list of installed NodeJS.
C:\>nvm list

     8.16.2
  1. Install another version of NodeJS using nvm install <版本号>the command
C:\>nvm install v12.8.0
Downloading node.js version 12.8.0 (64-bit)...
Complete
Creating C:\Dev\nvm\temp

Downloading npm version 6.10.2... Complete
Installing npm v6.10.2...

Installation complete. If you want to use this version, type

nvm use 12.8.0
  1. Use to nvm use <版本号>switch the NodeJS version to be used. After the switch is successful, you can use node -vthe and npm -vcommands to check whether the switch is successful.
C:\>nvm use v8.16.2
Now using node v8.16.2 (64-bit)

C:\>node -v
v8.16.2

C:\>npm -v
6.4.1

Note here that nvm use <版本号>there may be garbled characters when using

C:\>nvm use v8.16.2
exit status 1: ��û���㹻��Ȩ��ִ�д˲�����

Don't panic, don't be angry, at this time, if you reopen a cmd window running as an administrator, there will be no garbled characters. Use to nvm use <版本号>switch the NodeJS version you need to use, and use nvm ls to view all installed node versions. The version number is preceded by a *number Indicates the currently used NodeJS version.

C:\Windows\system32>nvm use v8.16.2
Now using node v8.16.2 (64-bit)

C:\Windows\system32>nvm ls
    12.8.0
  * 8.16.2 (Currently using 64-bit executable)
  1. Here is a summary of some commonly used commands:
nvm off                     // 禁用node.js版本管理(不卸载任何东西)
nvm on                      // 启用node.js版本管理
nvm install <version>       // 安装node.js的命名 version是版本号 例如:nvm install 8.12.0
nvm uninstall <version>     // 卸载node.js是的命令,卸载指定版本的nodejs,当安装失败时卸载使用
nvm ls                      // 显示所有安装的node.js版本
nvm list available          // 显示可以安装的所有node.js的版本
nvm use <version>           // 切换到使用指定的nodejs版本
nvm v                       // 显示nvm版本
nvm install stable          // 安装最新稳定版

Method 2: By configuring environment variables (just change the folder name when switching nodes)

Install node and precautions

  1. Similarly, if you have installed node before, in order to avoid unnecessary trouble, uninstall it first

Note : When installing multiple versions of node, you must start from the lower version. If you install the higher version first, there will be many problems. If you have already installed it, uninstall it first and then proceed to the following steps

  1. Before installing node, I first choose a node installation directory, and I want to install it under the D drive, so I created a new NODE directory in the D drive, and created a nodejs8 folder in it, because I will want to Install the 8.16.2 version of node in this folder
    insert image description here
  2. To start installing
    the node version, you can go to the node Chinese website or click here to download
    https://nodejs.org/
    to open the downloaded node8.16.2 installation package, and keep clicking Next until the installation path appears, and switch the installation path to the one we created above The nodejs8 folder path: D:\tools\NODE\nodejs8, and then follow the steps until the installation is successful

Configure environment variables

Search for environment variables in the start search box, note that here select Edit System Environment Variables— >Advanced—>Environment Variables,
insert image description here
insert image description here
click New in System Variables, variable name: nodejs, variable value: D:\tools\NODE\nodejs (that is, store node folder, here remove the 8 behind nodejs8 first)
insert image description here
insert image description here
Click OK and then find the path variable in the system variables, select it, click Edit, and then click New, add the variable name of the newly created system variable above in the form of and %变量名%click OK

insert image description here
It’s not working at this time, the most important point is: we need to rename the folder nodejs8 we built at the beginning to nodejs

It is worth noting that the renaming of nodejs8 here is to match the variable name in the environment variable configured above (that is, to use this version of node), and on the other hand, to better install the next version (because the installation After finishing a node version, if the file name is not changed, when you install other versions of node later, no matter where you install it, the previous one will be overwritten)

insert image description here

Check and install multiple versions

  1. examine

After renaming the name, then win+R—>cmd, enter in the command window node -vto view the node version, and the version number appears to indicate that the first node version is successfully installed

C:\>node -v
v8.16.2
  1. Install the next version
    Since the file name of the installed node8.16.2 version has been renamed above (to prevent overwriting), we only need to create a new folder nodejs12 in the same level directory, and install the node12.8.0 version of node (the method is the same as install node8.16.2 above)
    insert image description here

Node version switching

Here comes the important point, after installing multiple versions of node, when we need to switch versions, we only need to rename the folder of the corresponding version to nodejs

For example: We first installed node8.16.2 version (in the nodejs folder) and node12.8.0 version (in the nodejs12 folder), and the node we are using now is 8.16.2, so we want to switch to node12. For version 8.0 of node, just change the file name, rename nodejs to nodejs8, and then rename nodejs12 to nodejs
insert image description here
. When opening the command line window, enter node -vto check the node version

C:\>node -v
v12.8.0

Which method are you used to, friends? Have you learned both ways? Let's discuss together in the comment area!

Guess you like

Origin blog.csdn.net/qq_38970408/article/details/124474050