Node.js 安装配置
三大平台装 Node、验证安装、配置镜像源。
Key Points优先用版本管理器(nvm/fnm)装,不要用系统包管理器,否则切换版本会很痛苦。
三种安装方式
| Method | Pros | Cons |
|---|---|---|
| 官网安装包 | 双击即用 | 换版本要重装 |
| 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 22Output
$ 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 registryOutput
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.三种安装方式
| 方式 | 优点 | 缺点 |
|---|---|---|
| 官网安装包 | 双击即用 | 换版本要重装 |
| 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 22Output
$ 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 registryOutput
https://registry.npmmirror.com/装 Electron、Puppeteer 这类要下二进制的包时,光换 registry 不够,还要设对应的镜像变量:
Bash
npm config set electron_mirror https://npmmirror.com/mirrors/electron/
npm config set puppeteer_download_host https://npmmirror.com/mirrors/Windows 上如果
node -v 报「不是内部或外部命令」,先关掉所有终端重开,让 PATH 生效;仍不行就检查环境变量里有没有指向 nvm 的 symlink 目录。