Windows搭建nodejs环境

前言

本文将指导读者使用nvm以及nrm安装管理nodejs和npm源。

使用nvm可以方便的管理多个nodejs版本,该工具自动设置环境变量且无需关注全局模块保持位置。

使用nrm管理npm源,切换方便且集成多个源。

警告
使用nvm需要完全卸载已有的nodejs,请读者注意

nodejs环境安装

1 卸载安装抽象了的nodejs

  • 安装失败的

    • 删除nodejs相关文件
    • 删除相关环境变量
  • 安装成功的

    1. 清理目录

      1
      npm cache clean --force
    2. 卸载nodejs

      使用控制面板等方法删除nodejs

    3. 删除以下路径下的内容

      1
      2
      3
      4
      5
      6
      7
      8
      9
      {user}为你的用户名

      删除配置文件
      C:\User\{user}\.npmrc
      C:\Program Files (x86)\Nodejs
      C:\Program Files\Nodejs
      C:\Users\{User}\AppData\Roaming\npm (或%appdata%\npm)
      C:\Users\{User}\AppData\Roaming\npm-cache (或%appdata%\npm-cache)
      C:\Users\{User}\AppData\Local\Temp\npm-*
    4. 验证是否删除完成

      在cmd中输入

      1
      node -v

      此时无法输出nodejs版本相关信息即为删除完成。

2 安装NVM管理nodejs

2.1 安装nvm

下载址1(官网Github):点此下载

下载地址2(本站):点此下载

除了如下两处,其他无脑点击下一步即可

  • 设置NVM安装位置,nodejs实际位置,后继安装的nodejs会分版本保存在此处

image-20240301000004274

  • 设置nodejs虚拟位置,会创建一个软连接指向上一步选择的文件夹下的某一个nodejs文件夹

image-20240301000049886

一路下一步直到完成。

2.2 nvm的使用

2.2.1 查看可用版本

win+R输入__cmd__回车打开cmd,按照次序输入

1
2
# 查看可安装版本
nvm list available

image-20240302174526246

2.2.2 安装一个版本

使用如下命令安装一个nodejs版本

1
nvm install {version}

建议
我们建议安装和使用LTS版本,其稳定性和兼容性更佳

如下是安装完成(版本为18.19.1)

image-20240301000702064

1
nvm use 18.19.1

image-20240301000720347

2.2.3 其他nvm命令
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
nvm arch                      : Show if node is running in 32 or 64 bit mode.
nvm current : Display active version.
nvm debug : Check the NVM4W process for known problems (troubleshooter).
nvm install <version> [arch] : The version can be a specific version, "latest" for the
latest current version,or "lts" for the most recent
LTS 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 ofthis
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 whatcan 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 use "latest",
"lts", or "newest"."newest" is the latest installed 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
nvm arch                      : 显示Node.js是否在32位或64位模式下运行。 
nvm current : 显示当前版本。
nvm debug : 检查NVM4W进程是否存在已知问题(调试)。
nvm install <version> [arch] : 版本可以是一个具体的版本,可以是"latest"(最新版本)或
"lts"(最近发布的LTS版本)。可以选择安装32位或64位版本(默认为系统架构)。
使用"all"作为[arch]参数时,将同时安装32位和64位版本。
添加--insecure到命令末尾以跳过远程下载服务器的SSL验证。
nvm list [available] : 列出Node.js安装。在命令末尾添加"available"参数可以看到可安装的版本。
别名ls
nvm on : 启用Node.js版本管理。
nvm off : 禁用Node.js版本管理。
nvm proxy [url] : 设置下载代理。留空[url]查看当前代理。将[url]设置为"none"以移除代理。
nvm node_mirror [url] : 设置Node镜像。默认为https://nodejs.org/dist/>。留空[url]使用默认URL。
nvm npm_mirror [url] : 设置npm镜像。默认为https://github.com/npm/cli/archive/>。
留空[url]使用默认URL。
nvm uninstall <version> : 版本必须是具体的版本。
nvm use [version] [arch] : 切换到使用指定的版本。可以选择"latest"(最新版本)、"lts"(最近发布的
LTS版本)或"newest"(最新安装版本)。可选指定32位或64位架构。 使用
"newest"作为[arch]参数时,将继续使用当前版本,但切换到32位或64位模式。
nvm root [path] : 设置nvm存储不同版本Node.js的目录。不设置[path]时,当前根目录将显示。
nvm [--]version : 在Windows上显示当前运行的nvm版本。别名v。

3 使用nrm配置管理npm源

3.1 安装nrm

提示
在此之前,你可能需要更新你的npm。

1
npm install -g npm
1
npm i nrm -g

image-20240301000818375

3.2 使用淘宝源

1
nrm use taobao

image-20240301001010754

3.3 其他nrm命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Usage: cli [options] [command]

Options:
-V, --version : output the version number
-h, --help : display help for command

Commands:
ls : List all the registries
current [options] : Show current registry name or URL
use <name> : Change current registry
add <name> <url> [home] : Add custom registry
login [options] <name> [base64] : Set authorize information for a custom registry with
a base64 encoded string orusername and password
set-hosted-repo <name> <repo> : Set hosted npm repository for a custom registry to publish package
set-scope <scopeName> <url> : Associating a scope with a registry
del-scope <scopeName> : Remove a scope
set [options] <name> : Set a custom registry attribute
rename <name> <newName> : Change custom registry name
del <name> : Delete custom registry
home <name> [browser] : Open the homepage of registry with optional browser
test [registry] : Show response time for specific or all registries
help [command] : display help for command

以下是其中文翻译

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Usage: cli [options] [command]

Options:
-V, --version : 输出版本号
-h, --help : 显示命令帮助

Commands:
ls : 列出所有注册表
current [options] : 显示当前注册表名称或URL
use <name> : 更改当前注册表
add <name> <url> [home] : 添加自定义注册表
login [options] <name> [base64] : 为自定义注册表设置基于base64编码的字符串或用户名和密码的授权信息
set-hosted-repo <name> <repo> : 为自定义注册表设置托管npm仓库以发布包
set-scope <scopeName> <url> : 将作用域与注册表关联
del-scope <scopeName> : 删除作用域
set [options] <name> : 设置自定义注册表属性
rename <name> <newName> : 更改自定义注册表名称
del <name> : 删除自定义注册表
home <name> [browser] : 打开注册表的主页,可选浏览器
test [registry] : 显示特定或所有注册表的响应时间
help [command] : 显示命令帮助

结语

没啥可说的。