Use nvm to manage multiple versions of nodejs

foreword

Different projects depend on different versions of nodejs at work. For example, the related dependencies in the development of vue2 and vue3 are quite different, so it is necessary to switch between multiple node environments. The requirements can be well realized through nvm

1. Download and install nvm

1. It can be directly searched on github. Link address nvm download address

Windows users can choose an installation version to download
insert image description here

2. During installation, you need to choose the installation location of nvm and nodejs. The location of nvm is customized, and the location of nodejs is recommended to default.

insert image description here

After the installation is complete, NVM_HOME and NVM_SYMLINK are added to the system variables and user variables of the environment variables
insert image description here

NVM_HOME is the installation directory of nvm.
NVM_SYMLINK is the directory mapped by the current node, and it is a copy of the current nvm to the nodejs environment. In this way, node version switching is realized. Create an empty directory for yourself, and don't mix it with other directories .

3. Install nodejs environment

(1) If you have not installed it before, you can install it directly with nvm

For example nvm install 14.17.5
insert image description here

The nvm command has

nvm off                     // 禁用node.js版本管理(不卸载任何东西)
nvm on                      // 启用node.js版本管理
nvm install <version>       // 安装node.js的命名 version是版本号
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          // 安装最新稳定版node

(2) For those that have been installed before, decompress the zip format nodejs downloaded from the official website and rename the entire file to v-version number, then copy it to the nvm root directory, and use nvm use to switch the version.

insert image description here
insert image description here

2. Problems that may arise during use

1. An error message is reported when nvm use switches versions:

exit status 145: ��������ִ��������������ִ������������
exit status 1: ��û��� 㹻��Ȩ��ִ�д˲�����

In this case, open cmd with administrator privileges and then operate

3. Suggested configuration for nodejs installation.

Because after nodejs is installed, the npm global installation package and the generated cache will be saved in the c drive by default, occupying space, which is not conducive to maintenance. So the nodejs configuration needs to be optimized.

1. Find a directory in another disk and create two new folders node_cache and node_global

For example, I created in D:\software\nodeinsert image description here

2. Right-click My Computer, Properties, Advanced System Settings, Advanced/Environment Variables, create a new NODE_PATH in the system variables, and enter

​ D:\software\node\node_global\node_modules,
​user variable Path
delete the default C:\Users\wangxidong\AppData\Roaming\npm
add D:\software\node\node_global
insert image description here

3. Then execute the following two commands under the cmd command to set the global installation location and cache location of the npm package:

​ npm config set prefix "D:\work\node\node_global" // 设置全局包目录

​ npm config set cache "D:\work\node\node_cache" //设置缓存目录

View through npm config list command
insert image description here

4. In the future, the installation location of npm i xxx -g will be the current modified location

Guess you like

Origin blog.csdn.net/m0_72791534/article/details/128456592