A recent change in company and a new computer require a reconfiguration of the development environment.

background

As node.js and NPM versions are updated rapidly, some NPM packages or our project depends on different versions of Node and NPM, so we need to install the corresponding node version. Since the operation of uninstalling and reinstalling node is relatively cumbersome, we need a scheme that can install multiple node versions and realize switching.

Solution 1: Configure multiple environment variables and switch the Node version by changing the environment variables

Uninstall the node

If no node has been installed, skip this step. If a certain version of node has been installed, uninstall the corresponding node

  • If you have installed the Node installation package (.msi), perform the following steps

    1. Go to control Panel – Find program functions – Select Node – Perform uninstall
    2. Delete files related to Node
    C:\Program Files (x86)\Nodejs C:\Program Files\Nodejs C:\Users\{User}\AppData\ NPM (or % AppData %\ NPM) NPMRC :\Users\{User}\AppData\Roaming\npm-cache (or % AppData %\npm-cache) C:\Users\{User}\.npmrc (and may check no. C:\Users\{User}\AppData\Local\Temp\ NPM -*Copy the code
    1. Remove node and NPM references from environment variables
    2. Restart task Manager Kill all associated processes (or restart, as soon as possible)

Download multiple Node green installation packages

Node-v8.17.0-win-x64 is a stable version of Node-v8.17.0-win-x64. Zip, node-v10.22.1-win-x64.zip, node-v12.19.0-win-x64.zip

Configuring system Variables

  • Configure node_8, node_10, and node_12 in the system variables, and their addresses point to the node green installation package of the corresponding version

  • Set the system variable node_global, which points to a node version of the variable we defined in the previous step

  • Add the variable %node_global% after the system variable path

  • Command line to change system variables (node_global is manually changed because the node path in path is not in effect)
Set node_global=%node_10% echo %node_global% // Current node_global address echo %path% // Current PATH addressCopy the code
  • The command line modification of system variables takes effect only in the current CMD window

Solution 2: Use NVM to switch between node versions

Unmount an existing node

Download the NVM-Windows installation package

NVM download address: github.com/coreybutler…

The installation

Nvm-setup installation steps

  • Unzip nVM-setup. zip, double-click nVM-setup. exe to install,

  • Set the NVM installation directory, the default path C:\Users\ Wangyue \AppData\Roaming\ NVM, here I add to disk D

  • Set the node installation path, default path: C:\Program Files\nodejs, here I add disk D

  • After the installation is successful, enter NVM -v and output the version number

Nvm-setup green free installation

  • Unpack the NVM – noinstall; It’s called NVM, and I put it on drive D

  • Create an NVM to store the current node storage path
// NVM :\Program Files\ NVM // NVM puts the current node storage path D:\Program Files\NodeJsCopy the code
  • Create two new system variables
NVM_HOME: D:\Program Files\ NVM // points to nvm.exe NVM_SYMLINK: D:\Program Files\NodeJs // points to Node.exeCopy the code

  • Add %NVM_HOME% to the system variable path; %NVM_SYMLINK%;

  • Add settings. TXT in the NVM directory. See the example on Github
Root: D: Program Files\ NVM // NVM address to store multiple versions of node path: D: Program Files\NodeJs // To store current use of node arch: Proxy: None // Set the proxyCopy the code

Note: When installing or switching versions, NVM does not automatically identify which version of Node is suitable for the operating system, so it is best to declare it yourself

NVM sets taobao image

Node_mirror changes the mirror used by NVM to download node. If this parameter is not set, the default value is nodejs.org/dist/

Npm_mirror is the mirror that changes the NPM download dependencies. If not set, default github.com/npm/npm/arc… .

  • Method one, use CMD
nvm node_mirror: https://npm.taobao.org/mirrors/node/
nvm npm_mirror: https://npm.taobao.org/mirrors/npm/
Copy the code
  • Method 2: Modify setting.txt
//setting.txt
node_mirror: https://npm.taobao.org/mirrors/node/  
npm_mirror: https://npm.taobao.org/mirrors/npm/
Copy the code

NVM Changes the node version

  • Use NVM list available; View the available Node versions

  • Use NVM install [version] to install the corresponding version of Node. I downloaded three versions 8.17.0, 10.22.1 and 12.19.0

  • Use NVM use [version] to switch to the specified version

Here, I have “Exit Status 1…” Error: I installed NVM in the path with Spaces (D:\Program Files), so the switchover failed. Switch to the path without Spaces, and put in the system variable that needs to be reconfigured and setting.text

Description of NVM commands

NVM install Install the latest version NVM NVM version/ NVM current Check the current NVM version NVM list available: View the available node versions. NVM list/ NVM ls View the installed versions. NVM install <version> Installs a specific node version <version> ## Switch to the specified version of Node NVM use [version] [arch] switch to the specified version of Node NVM scant-packages <version> ## NVM on enable nodeJS control NVM off Disable nodejs control NVM alias <name> <version> # NVM proxy [url] set node_mirror in setting. TXT https://nodejs.org/dist/ NVM npm_mirror [url] Npm_mirror in setting. TXT https://github.com/npm/npm/archive/. NVM root/path Settings, and view the root pathCopy the code

NRM (NPM Registry Manager) – NPM image source management tool

This section describes an image source management tool that can quickly switch between NPM image sources

  • Install NRM globally
npm install -g nrm
Copy the code
  • View the source addresses of all mirrors in the NRM. * is the current source in use

  • NRM use [registry]

Switch to taobao mirror source

  • NRM command
NRM -h/NRM -help // Lists the commands and information about the NRM. NRM -v // VERSION of the NRM NRM ls // Lists all the source addresses of the NRM. NRM current // Use <registry> // NRM add <registry> < URL > [home] // Add a mirror source; Registry Indicates the source name of the image. Url mirror source address NRM del <registry> // Delete the corresponding source. NRM test <registry> // Tests the image source download response timeCopy the code