The use of nvm (node version control tool) and nrm (npm source manager)

Foreword: In the actual development of the project, multiple projects will be developed in parallel, and different projects often have different development environments. A single environment will often encounter node upgrade/downgrade, npm upgrade/downgrade, and the incompatibility between node and npm. Problems such as compatibility and dependency package installation are slow or stuck, which affects development efficiency. How to solve this problem? Then we will introduce the use of nvm and nrm.

1. nvm download and installation

Note: Before installing and using nvm, be sure to uninstall all node.js versions that already exist in the system! ! !

  1. nvm-windows official website
    Insert image description here

  2. Unzip nvm-setup.zip and double-click nvm-setup.exe to install. You can customize the installation directory or default the installation directory (write down the installation directory, you will use it later to modify the environment variables~).
    Insert image description here

nvm installation directory:
Insert image description here

nodejs installation directory:
Insert image description here

2. Find the settings.txt file in the C:\nvm directory and modify the content of settings.txt (mainly to solve the problem when nvm installs node, so that the corresponding installation of node and npm is successful) a>

root: C:\nvm
path: C:\Program Files\nodejs
// 将npm镜像改为淘宝的镜像,可以提高下载速度(虽然也可以用,但是建议还是用新的淘宝镜像!)
//node_mirror: https://npm.taobao.org/mirrors/node/
//npm_mirror: https://npm.taobao.org/mirrors/npm/
// 原taobao镜像地址修改最新的npmmirror.com
node_mirror: https://npmmirror.com/mirrors/node/
npm_mirror: https://npmmirror.com/mirrors/npm/

Note:npmmirror mirror station
Insert image description here

3. Modify and configure nvm environment variables (mainly to be able to use nvm related instructions)

Add to user variables

NVM_HOME C:\nvm
NVM_SYMLINK C:\Program Files\nodejs

Insert image description here

Add to path

Path %NVM_HOME%;%NVM_SYMLINK%

Attention! ! !
%NPM_HOME% should be added before %NVM_SYMLINK% to prevent npm from accessing the npm package management tool that comes with nodejs

Insert image description here

4. Check whether nvm is successfully installed and use common commands related to nvm.

  1. Check whether nvm is installed successfully
    Open cmd and enter nvm, as shown below, the installation is successful!
    Insert image description here

  2. nvm other commands

nvm -v // 查看nvm版本号

Insert image description here

nvm list available // 查看能安装node的版本有哪些

Insert image description here

nvm install latest // 安装node最新版本
nvm install 19.8.0 // 安装node指定版本19.8.0
nvm uninstall 19.8.0 // 卸载[email protected]

Insert image description here

nvm use 19.8.0 // 使用node版本19.8.0

Insert image description here

nvm ls // 查看本地已安装了node哪些版本

Insert image description here

  1. Check whether node and corresponding npm are installed successfully
  • Method 1: Check the node and npm version numbers
node -v
npm -v

As shown in the figure, the installation is successful

image.png

  • Method 2: Go directly to the nvm installation directory to view the node installation directory. The picture shows that the installation is successful
    image.png

5. Install npm globally

Under normal circumstances, when installing node, npm is also installed together. Every time you install a node version, you need to install a bunch of packages. It would be much more convenient if there was an npm that could be shared by all versions of node. This can be achieved by configuring a global npm.

  1. Configure the package path for global installation when downloading packages with npm
npm config set prefix "C:\nvm\npm"
  1. Configure environment variables
    Add in user variables
NPM_HOME C:\nvm\npm

23.png

Add to path

Path %NPM_HOME%

24.png

6. nrm manages npm sources

When there is only one npm source to download dependency packages, domestic download speeds may slow down or freeze, or even fail to download. If you use yarn to download project dependency packages, project maintenance problems will arise. You use yarn, while other partners use npm. , making the development project environment different, resulting in various running/packaging problems when running the project.

At this time, you need to find another way to solve it-change the npm source.

To change the npm source, you can use the registry to configure the npm source, but you need to remember the address, so it will be more efficient for lazy people to choose nrm for management.

  1. Install nrm globally
npm install nrm -g --save
  1. Check whether nrm is installed successfully
nrm ls // npm源列表
nrm current // 当前使用的npm源
nrm use taobao // 切换淘宝源

As shown below, the installation is successful!
image.png

  1. After switching the npm source to Taobao source, you can still use the npm command.
npm i @vue/cli -g

This download speed has been improved to a new level!
image.png

  1. Problems encountered when using nrm

(1) npm version problem
29.png

solution:

Upgrade npm package

npm i -g npm@9.6.5

(2) Updating the npm package still doesn’t work, as shown in the figure:
Insert image description here

solution:

Uninstall the original globally installed nrm and reinstall the specified version.

npm install -g nrm open@8.4.2 --save

nrm was successfully installed and used!
Insert image description here

7. A collection of problems encountered during the installation and use of nvm (avoid pitfalls!!!)

The installation of nodejs must be successful in both npm and node, otherwise it will not be considered a successful installation of nodejs.

  • Question 1: When nvm installs node, node installation is successful but npm installation fails.

Insert image description here

solution:

(1) Add the image to the setting.txt file in nvm (do not add https), and find that npm is still not installed successfully.

node_mirror: npm.taobao.org/mirrors/node/
npm_mirror: npm.taobao.org/mirrors/npm/

(2) If the mirror is not available, manually download the package corresponding to the required node version to the nvm directory.

Download address:https://nodejs.org/download/release/

Insert image description here

  • Problem 2: node is not recognized as an internal or external command, operable program or batch file

solution:

(1) Modify settings.txt in the nvm directory (especially if you customize nvm installation, you need to pay attention)
Insert image description here

(2) Set environment variables

  • Question 3: nvm use xxx appears with exit status garbled code

Solution:
The main reason is that cmd does not have administrator rights to run. You can use administrator rights to operate cmd (provided that node and npm have been installed). And the node image path is correct).

  • Question 4:exit status 1: Access is denied. // 拒绝访问

Insert image description here

solution:

When cmd is running, just run it as administrator.

  • Question 5:nvm use: Cannot create a file when that file already exists.

Insert image description here

During the installation of nvm, you need to set the shortcut connection symlink of nodejs. The default for windows is C:\Program Files\nodejs. nvm changes the node version by modifying the shortcut link.

solution:

(1) The error prompted here means that the nodejs link already exists, and the original one needs to be deleted; if you are afraid of problems with deletion, you can rename the original nodejs. Then use nvm use switch again.

(2) Check whether there is already a default option in the system variables. If there is one more, delete it.

  • Question 6: 'nvm' is not recognized as an internal or external command, operable program or batch file

Solution:
Add nvm to the system variables.
Insert image description here

Guess you like

Origin blog.csdn.net/acai_encode/article/details/130625095