Node.js 安装指南
为 Claude Code 提供 Node.js 运行环境,本指南涵盖 macOS、Windows、Linux 多个平台的详细安装方法。
ℹ️
推荐版本:Node.js 18.x LTS 或更高版本,确保稳定性和兼容性。
macOS 安装
方法一:使用 Homebrew(推荐)
首先安装 Homebrew(如果未安装):
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
然后安装 Node.js:
# 更新 Homebrew
brew update
# 安装 Node.js(LTS 版本)
brew install node
方法二:官网下载
- 访问 Node.js 官网 (opens in a new tab)
- 下载 LTS 版本的 macOS 安装包
- 双击
.pkg
文件完成安装
验证安装
# 检查 Node.js 版本
node --version
# 检查 npm 版本
npm --version
✅
期望看到类似输出:v18.17.0
和 9.6.7
Windows 安装
方法一:官网下载(推荐)
- 访问 Node.js 官网 (opens in a new tab)
- 点击下载 LTS 版本(长期支持版)
- 下载 Windows Installer (.msi) 文件
- 双击运行安装程序
- 安装过程中保持默认设置,确保勾选 "Add to PATH"
方法二:使用 Chocolatey
如果你已安装 Chocolatey 包管理器:
# 以管理员身份运行 PowerShell
choco install nodejs
验证安装
打开命令提示符或 PowerShell:
# 检查 Node.js 版本
node --version
# 检查 npm 版本
npm --version
✅
期望看到类似输出:v18.17.0
和 9.6.7
Linux 安装
Ubuntu/Debian 系统
方法一:使用 NodeSource 仓库(推荐)
# 更新系统包
sudo apt update
# 安装 curl
sudo apt install -y curl
# 添加 NodeSource 仓库
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
# 安装 Node.js
sudo apt-get install -y nodejs
方法二:使用 Snap
# 安装 Node.js LTS 版本
sudo snap install node --classic
CentOS/RHEL/Fedora 系统
CentOS/RHEL 8+
# 启用 NodeJS 模块
sudo dnf module enable nodejs:18
# 安装 Node.js 和 npm
sudo dnf install nodejs npm
Fedora
# 安装 Node.js 和 npm
sudo dnf install nodejs npm
Arch Linux 系统
# 更新系统
sudo pacman -Syu
# 安装 Node.js 和 npm
sudo pacman -S nodejs npm
WSL2 环境
# 更新 WSL2 系统
sudo apt update && sudo apt upgrade
# 使用 NodeSource 方法安装(推荐)
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt-get install -y nodejs
验证安装
# 检查 Node.js 版本
node --version
# 检查 npm 版本
npm --version
✅
期望看到类似输出:v18.17.0
和 9.6.7
故障排除
常见问题
权限问题(Linux/macOS)
如果遇到 npm 安装全局包的权限问题:
# 创建全局目录
mkdir ~/.npm-global
# 配置 npm 使用新目录
npm config set prefix '~/.npm-global'
# 添加到环境变量
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
Windows PATH 问题
如果 node
或 npm
命令不被识别:
- 重新启动命令提示符或 PowerShell
- 检查系统环境变量中是否包含 Node.js 路径
- 如果仍有问题,重新安装 Node.js 并确保勾选 "Add to PATH"
版本冲突
如需管理多个 Node.js 版本:
Linux/macOS: 使用 nvm
# 安装 nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
# 重新加载终端
source ~/.bashrc
# 安装并使用特定版本
nvm install 18
nvm use 18
Windows: 使用 nvm-windows
- 从 nvm-windows 发布页面 (opens in a new tab) 下载安装程序
- 安装后使用类似命令管理版本
🎉
Node.js 安装完成!现在可以继续安装 Claude Code 了。
下一步
Node.js 安装完成后,请返回对应的安装指南继续后续步骤:
- macOS 安装指南 - 继续 Claude Code 安装
- Windows 安装指南 - 继续 Claude Code 安装
- Linux 安装指南 - 继续 Claude Code 安装