Node.js 是一个基于 Chrome V8 JavaScript 引擎的 JavaScript 运行时环境,可以让 JavaScript 脱离浏览器直接运行。
如果你需要开发 Node.js 后端、Electron、前端项目、Vite、Vue、React、npm 工具等项目,通常都需要先安装 Node.js。
本文主要介绍:
安装 Node.js LTS 长期支持版
配置国内 npm 镜像加速
使用
--registry https://registry.npmmirror.com验证 Node.js 和 npm 是否安装成功
修改 npm 默认 Registry
临时使用镜像
恢复官方 npm Registry
Node.js 版本管理
常见问题排查
一、为什么推荐安装 LTS 版本?
Node.js 通常会提供不同的版本线路,其中比较重要的是:
LTS(Long Term Support):长期支持版本
Current:当前版本,更新速度更快
如果你是:
普通开发者
搭建服务器
开发 Electron 应用
开发 Vue / React 项目
搭建前端项目
运行第三方 Node.js 工具
那么一般推荐直接使用 LTS 版本。
LTS 版本更加适合长期使用,生态兼容性通常也更好。
二、检查系统中是否已经安装 Node.js
安装之前,可以先检查一下。
打开终端:
node -v
或者:
node --version
检查 npm:
npm -v
如果已经安装,会看到类似:
v24.x.x
以及:
11.x.x
如果提示:
command not found: node
或者:
node: command not found
说明系统中没有安装 Node.js,或者 Node.js 没有加入 PATH。
三、Linux 安装 Node.js LTS
方法一:使用 NodeSource 安装
对于 Ubuntu / Debian 等 Linux 系统,可以使用 NodeSource 提供的安装方式。
首先更新软件包:
sudo apt update
安装必要工具:
sudo apt install -y curl
然后添加 Node.js LTS 软件源。
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
安装 Node.js:
sudo apt install -y nodejs
安装完成以后检查:
node -v
npm -v
如果正常输出版本号,就说明安装成功。
四、Linux 使用 NVM 安装 Node.js LTS
如果你经常需要切换 Node.js 版本,更推荐使用 NVM(Node Version Manager)。
NVM 可以让你同时安装多个 Node.js 版本,例如:
Node.js 20
Node.js 22
Node.js 24
然后根据项目需要随时切换。
1. 安装 NVM
执行:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash
安装完成以后重新打开终端。
也可以手动加载:
source ~/.bashrc
如果你使用的是 Zsh:
source ~/.zshrc
检查 NVM:
nvm --version
如果看到类似:
0.40.x
就说明 NVM 安装成功。
2. 安装 Node.js LTS
直接执行:
nvm install --lts
然后启用 LTS:
nvm use --lts
检查:
node -v
npm -v
3. 将 LTS 设置为默认版本
如果你希望以后打开终端自动使用 LTS:
nvm alias default 'lts/*'
然后:
nvm use --lts
以后重新打开终端,默认就会进入 LTS 版本。
五、Windows 安装 Node.js LTS
Windows 用户可以直接安装 Node.js 官方安装程序。
下载安装程序以后,一路按照安装向导进行即可。
安装完成后打开:
PowerShell
或者:
CMD
执行:
node -v
然后:
npm -v
如果能够正常输出版本号,就说明安装成功。
Windows 更推荐使用 NVM for Windows
如果你经常需要切换 Node.js 版本,可以使用 NVM for Windows。
安装完成后,可以使用:
nvm list available
查看可以安装的 Node.js 版本。
安装某个版本:
nvm install 24
然后:
nvm use 24
检查:
node -v
六、macOS 安装 Node.js LTS
macOS 用户同样推荐使用 NVM。
安装 NVM:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash
重新加载 Shell:
source ~/.zshrc
检查:
nvm --version
安装 LTS:
nvm install --lts
使用 LTS:
nvm use --lts
设置默认版本:
nvm alias default 'lts/*'
最后检查:
node -v
npm -v
七、配置 npm 镜像加速
安装 Node.js 以后,npm 默认使用官方 Registry。
如果访问 npm 官方 Registry 的速度比较慢,可以使用 npmmirror 镜像。
npmmirror 的 Registry 地址:
https://registry.npmmirror.com
八、临时使用 npmmirror 镜像
如果你只想让某一次 npm 安装使用镜像,可以使用:
npm install --registry https://registry.npmmirror.com
例如安装:
npm install express --registry https://registry.npmmirror.com
安装 Vue:
npm install vue --registry https://registry.npmmirror.com
安装 Vite:
npm install vite --registry https://registry.npmmirror.com
九、npm install 时使用镜像
例如一个已经存在的 Node.js 项目:
cd my-project
然后:
npm install --registry https://registry.npmmirror.com
npm 会从 npmmirror 下载依赖,而不是使用默认 Registry。
这种方法不会修改 npm 的全局配置。
也就是说:
npm install
以后依然会使用默认 Registry。
十、将 npmmirror 设置为 npm 默认镜像
如果你经常使用 npmmirror,推荐直接修改 npm 的 Registry。
执行:
npm config set registry https://registry.npmmirror.com
然后检查:
npm config get registry
应该看到:
https://registry.npmmirror.com/
之后执行:
npm install
npm 默认就会使用 npmmirror。
这样以后就不需要每次都写:
--registry https://registry.npmmirror.com
十一、推荐的完整配置方式
如果你刚刚安装完 Node.js,并且希望直接配置 npm 镜像,可以执行:
npm config set registry https://registry.npmmirror.com
然后:
npm config get registry
确认输出:
https://registry.npmmirror.com/
最后测试安装一个包:
npm install express
如果能够正常下载,就说明配置成功。
十二、临时切换回官方 Registry
如果某个项目要求使用 npm 官方 Registry,可以临时指定:
npm install --registry https://registry.npmjs.org
也可以永久恢复:
npm config set registry https://registry.npmjs.org
检查:
npm config get registry
正常情况下:
https://registry.npmjs.org/
十三、查看 npm 当前所有配置
可以执行:
npm config list
如果想查看更加详细的信息:
npm config list -l
查看当前 Registry:
npm config get registry
十四、查看 Node.js 和 npm 的安装位置
Linux / macOS:
which node
which npm
例如:
/usr/bin/node
/usr/bin/npm
如果使用 NVM,则可能类似:
/home/username/.nvm/versions/node/v24.x.x/bin/node
Windows:
where node
where npm
十五、检查 Node.js 是否能够正常运行
可以直接执行:
node
进入 Node.js REPL。
例如:
> 1 + 1
2
再测试:
> console.log("Hello Node.js")
Hello Node.js
退出:
.exit
或者按:
Ctrl + D
十六、使用 npm 创建一个简单项目
创建目录:
mkdir node-demo
进入:
cd node-demo
初始化 npm 项目:
npm init -y
会生成:
package.json
然后安装 Express:
npm install express
如果使用 npmmirror:
npm install express --registry https://registry.npmmirror.com
如果已经将 Registry 设置为 npmmirror:
npm install express
即可。
十七、npm 全局安装工具
有些 Node.js 工具需要全局安装。
例如:
npm install -g pnpm
如果当前 npm 已经配置为 npmmirror:
npm install -g pnpm
也可以明确指定:
npm install -g pnpm --registry https://registry.npmmirror.com
检查:
pnpm -v
十八、推荐使用 Corepack 管理 pnpm / Yarn
对于较新的 Node.js 版本,可以使用 Corepack 管理包管理器。
例如启用 Corepack:
corepack enable
然后可以使用:
corepack prepare pnpm@latest --activate
检查:
pnpm -v
不过需要注意,具体 Corepack 行为会随着 Node.js 版本变化。
如果项目已经明确要求某个 pnpm 版本,优先按照项目的 package.json 或官方文档执行。
十九、npm 镜像相关常用命令
查看当前 Registry
npm config get registry
设置 npmmirror
npm config set registry https://registry.npmmirror.com
恢复官方 Registry
npm config set registry https://registry.npmjs.org
临时使用 npmmirror
npm install --registry https://registry.npmmirror.com
临时安装指定包
npm install <package-name> --registry https://registry.npmmirror.com
例如:
npm install axios --registry https://registry.npmmirror.com
二十、遇到 npm 下载异常怎么办?
如果 npm 出现:
npm ERR!
或者:
ETIMEDOUT
ECONNRESET
network error
可以首先检查 Registry:
npm config get registry
然后尝试:
npm ping
如果使用 npmmirror:
npm ping --registry https://registry.npmmirror.com
也可以清理 npm 缓存:
npm cache clean --force
然后重新安装:
npm install
二十一、如果 npm 命令不存在
如果执行:
npm -v
提示:
npm: command not found
首先检查:
node -v
如果 Node.js 也不存在,说明 Node.js 没有正确安装。
如果 Node.js 存在而 npm 不存在,则需要检查 Node.js 安装是否完整,以及 PATH 环境变量。
如果使用 NVM,可以尝试:
nvm use --lts
然后:
node -v
npm -v
二十二、为什么推荐 NVM?
如果你只是运行一些简单的 Node.js 程序,直接安装 Node.js LTS 就够了。
但是如果你经常开发不同项目,NVM 会非常方便。
例如:
nvm install 20
nvm install 22
nvm install 24
查看已经安装的版本:
nvm ls
切换到 Node.js 20:
nvm use 20
切换到 Node.js 22:
nvm use 22
切换到 LTS:
nvm use --lts
这样不同项目就可以使用不同版本的 Node.js,而不会互相影响。
二十三、推荐安装流程
如果是 Linux 用户,我比较推荐下面这套流程:
1. 安装 NVM
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash
重新加载:
source ~/.bashrc
2. 安装 Node.js LTS
nvm install --lts
3. 使用 LTS
nvm use --lts
4. 设置默认版本
nvm alias default 'lts/*'
5. 配置 npm 镜像
npm config set registry https://registry.npmmirror.com
6. 检查 Node.js
node -v
7. 检查 npm
npm -v
8. 检查 Registry
npm config get registry
最终应该能够看到类似:
Node.js: v24.x.x
npm: 11.x.x
Registry: https://registry.npmmirror.com/
二十四、一键执行版
如果已经安装好 NVM,可以直接执行:
nvm install --lts
nvm use --lts
nvm alias default 'lts/*'
npm config set registry https://registry.npmmirror.com
然后检查:
node -v
npm -v
npm config get registry
二十五、最终效果
完成配置以后,一个典型的 Node.js 开发环境应该类似:
Node.js
│
├── node
│
└── npm
│
└── Registry
│
└── https://registry.npmmirror.com/
以后安装项目依赖时,直接:
npm install
安装指定依赖:
npm install express
开发环境工具:
npm install -g pnpm
都可以直接使用已经配置好的镜像。
总结
如果只是想快速安装 Node.js LTS,推荐:
nvm install --lts
然后:
nvm use --lts
设置默认版本:
nvm alias default 'lts/*'
最后配置 npm 镜像:
npm config set registry https://registry.npmmirror.com
检查:
node -v
npm -v
npm config get registry
如果只想让某一次安装使用镜像,则不需要修改全局配置,直接:
npm install --registry https://registry.npmmirror.com
即可。
推荐方案:NVM + Node.js LTS + npmmirror
这种组合对于需要同时维护多个 Node.js 项目的开发环境来说比较灵活,也方便以后升级或切换 Node.js 版本。