Install and configure nvm-windows for version control of Node.js and npm

One, nvm

Due to the Node.js version, some errors may occur, such as the npm error Error:0308010C:digital envelope routines:unsupported in the Node.js environment in IDEA . Moreover, different projects use different versions of Node.js, so it is too cumbersome to uninstall and configure again. So you can use nvm to dynamically and flexibly manage the version of Node.js.

Note: The Win7 system does not support the latest version of Node.js. I am currently testing on another machine, and the highest version can only use Node.js13.14.0.
Node.js is only supported on Windows 8.1, Windows server 2012 R2, or higher.

#nvm:管理Node.js版本的工具,可以管理多个Node.js版本以及其对应的npm。

#Node.js:一个基于 Chrome V8 引擎的 JavaScript 运行环境,是一个开源的、跨平台的 JavaScript 运行时环境。Node.js 使用了一个事件驱动、非阻塞式 I/O 的模型,使其轻量又高效。

#npm:Node.js 的包管理器 npm,是目前最流行的Node.js 的包管理器。

#npm、Node.js、npm三者关系的总结:nvm管理Node.js的版本,npm管理Node.js的包。

Note: If you have installed and used Node.js on your own machine before, you must uninstall the installed version first, otherwise it will not be included in the version management of nvm, and conflicts may occur.
insert image description here
insert image description here

Enter node -v and npm -v in cmd to check the versions of Node.js and npm respectively, and the uninstallation is complete only when the following prompt appears:
insert image description here

2. Download and install nvm

2.1 Download nvm-windows

Click the access link to download nvm-windows

Download nvm-setup.exe or nvm-set.zip (unzip is also nvm-setup.exe) .
insert image description here
Finally, you will get such an installation package :
insert image description here

2.2 Install nvm-windows

insert image description here

insert image description here
Note: This Node.js soft link folder may not appear immediately after the nvm-windows installation is completed. It will appear only after you use the nvm install command to install a certain Node.js version or switch versions. Check whether the nvm installation is successful : nvm -v nvm installation directory content: User variables automatically configured by default during nvm installation:automatically configured by default during nvm installation Path :
insert image description here
insert image description here
insert image description here

insert image description here

insert image description here

insert image description here
insert image description here

insert image description here

2.3 Configure nvm taobao image

Use the editor software to open the settings.txt folder under the nvm installation directory , and add the following taobao image to it: Note: The image source set here is the source for installing Node.js and npm itself, not the source for installing the Node.js package.

##node taobao mirrors
node_mirror: https://npm.taobao.org/mirrors/node/

##npm taobao mirrors
npm_mirror:  https://npm.taobao.org/mirrors/npm/

insert image description here
Default content of settings.txt :
insert image description here
settings.txt after adding taobao image:
insert image description here

2.4 Create new node_global and node_cache

Create a new node_global & node_cache folder in the Node.js installation directory

#node_global是npm的全局模块的路径
#node_cache是npm使用时的cache即缓存的配置路径。

insert image description here

3. Configure nvm, Node.js, and npm to work together

3.1 Using nvm

List of commonly used nvm commands:

#nvm   -v                       查看nvm版本
#nvm                            查看nvm相关信息
#nvm   list  available          查看可用的版本
#nvm   install Node.js的版本号   安装Node.js18.16.0 (如nvm install 18.16.0)
#nvm   use    Node.js的版本号    使用Node.js18.16.0 (如nvm use 18.16.0)  如果安装了多个Node.js包,就相当于切换版本。 【只会安装不会安装并自动切换到这个版本】
#nvm   list或nvm ls             查看已下载的Node.js的版本

#notice:这里使用的较少
#nvm arch                       查看当前Node.js运行64位或32位操作系统上
#nvm on/off                     开启/关闭Node.js的版本管理
#nvm use 版本号 arch             使用指定版本指定位数的Node.js
#nvm uninstall                  卸载指定版本的Node.js

nvm -v View nvm version:
insert image description here
nvm View nvm related information:
insert image description here
nvm list available and nvm install Node.js version number:
insert image description here
Node.js official website shows that the long-term maintenance version is 18.16.0 and the latest early adopter version 20.3.0 and nvm list available to obtain consistent with:

insert image description here
###Install and switch to Node.js 20.3.0 whole process###:

Version numbers for nvm list and nvm use Node.js :
insert image description here

#查看使用的Node.js的版本
node -v

#查看Node.js对应的npm的版本
npm -v

#查看npm config配置的信息
npm config list

3.2 npm config set registry set the mirror station

You can refer to my
IDEA in the Node.js environment npm error Error: 0308010C: digital envelope routines: unsupported

Use the npm config set command in nmp config in CMD to configure the mirror-regisry Note: The mirror source set here is the source for installing the Node.js package.

#npm config set registry 设置镜像站
npm config set registry=http://registry.npm.taobao.org

insert image description here
###Note: From the above operation, we can draw a conclusion: once the registry is set, it will have an effect on all the Node.js version packages you have installed###

3.3 npm config set configure node_global and node_cache

Configure the node_global and node_cache folders in CMD using the npm config set command in nmp config

npm config set prefix "D:\front\nvm\node_global"
npm config set cache  "D:\front\nvm\node_cache"
#node_global是npm的全局模块的路径
#node_cache是npm使用时的cache即缓存的配置路径。

insert image description here

3.4 npm config get Get npm related configuration information

#npm config list            查看npm config配置的信息
#npm config get cache       获取cache配置的信息 (npm使用时的cache即缓存的配置路径)
#npm config get prefix      获取npm的全局模块的路径
#npm config get registry    获取镜像信息

#经过测试下列简化命令也适用
#npm  get cache       获取cache配置的信息 (npm使用时的cache即缓存的配置路径)
#npm  get prefix      获取npm的全局模块的路径
#npm  get registry    获取镜像信息

insert image description here
#After testing, the following simplified commands are also applicable [this step can be ignored]
insert image description here

Fourth, use nvm, Node.js, npm collaborative workflow

Currently ( 2023-6-22 17:28:46 ) the long-term maintenance version of Node.js is 18.16.1 (recommended for most users) and the latest early adopter version 20.3.1 (including the latest features) :
insert image description here
insert image description here

insert image description here

4.1 Process

Note: The Win7 system does not support the latest version of Node.js. I am currently testing on another machine, and the highest version can only use Node.js13.14.0.
The Win7 system uses the latest version of Node.js20.3.1 and an error occurs :
insert image description here
The Win7 system uses the long-term support version Node.js18.16.1 and an error occurs:
Node.js is only supported on Windows 8.1,Windows server 2012 R2, or higher.
insert image description here
The Win7 system uses Node. js13.14.0:
insert image description here

#1、通过nvm查看可用的Node.js的版本
nvm   list  available 

#2、查看目前已经安装Node.js的版本(查看已下载的Node.js的版本)
#nvm list  或 nvm ls           

#3、 安装需要的Node.js的版本 (如nvm install 18.16.1)【只会安装不会安装并自动切换到这个版本】
 #nvm   install Node.js的版本号  

#4、使用此版本
#nvm   use    Node.js的版本号    
使用Node.js 18.16.1 (如nvm use 18.16.1) 如果安装了多个Node.js包,就相当于切换版本。


#5、切换版本【同上,可忽略】
nvm   use    Node.js的版本号    

Screenshot of the whole process using Node.js 20.3.1 [Switch from 18.16.1 to 20.3.1]: Screenshot of the whole process of
insert image description here
using Node.js 18.16.1 [Switch from 20.3.1 to 18.16.1]:
insert image description here

4.2 View the relevant configuration information of the Node.js version

Configuration information of Node.js18.16.1:
insert image description here
Configuration information of Node.js20.3.1:
insert image description here
So: cache, prefix, registry settings only need to be configured once, similar to global variables.
The cache, prefix, and registry are configured as described above, and these information are stored in the .npmrc file:
insert image description here
insert image description here

4.3 The nvm file directory that has used nvm to switch Node.js versions

insert image description here
insert image description here
insert image description here

5. References

nvm-sh
coreybutler/nvm-windows
nvm-windows download and install and step on the pit
Index of /npm/
Windows nvm installation and use
nvm for windows download, install and use
npm error Error:0308010C:digital envelope routines under Node.js environment in IDEA :unsupported

Guess you like

Origin blog.csdn.net/qyfx123456/article/details/131341906