Node.js 安装配置

三大平台装 Node、验证安装、配置镜像源。

Beginner
Key Points优先用版本管理器(nvm/fnm)装,不要用系统包管理器,否则切换版本会很痛苦。

三种安装方式

MethodProsCons
官网安装包双击即用换版本要重装
nvm / fnm一条命令切版本多一层 shell 配置
系统包管理器apt install nodejs版本通常很旧

用 nvm 装(推荐)

Bash
# Linux / macOS
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
source ~/.bashrc

nvm install 22
nvm use 22
nvm alias default 22
Output
$ nvm ls
->     v22.22.2
default -> 22 (-> v22.22.2)
node -> stable (-> v22.22.2)
POWERSHELL
# Windows:用 nvm-windows
winget install CoreyButler.NVMforWindows

nvm install 22
nvm use 22

验证

Bash
node -v
npm -v
node -e "console.log(process.platform, process.arch)"
Output
v22.22.2
10.9.7
win32 x64

换国内镜像

Bash
npm config set registry https://registry.npmmirror.com
npm config get registry
Output
https://registry.npmmirror.com/

For packages that download binaries — Electron, Puppeteer — changing the registry is not enough; set the matching mirror variables too:

Bash
npm config set electron_mirror https://npmmirror.com/mirrors/electron/
npm config set puppeteer_download_host https://npmmirror.com/mirrors/
On Windows, if node -v says the command is not recognized, close every terminal and reopen so PATH refreshes; if it still fails, check that PATH contains nvm's symlink directory.

Friends & Partners