2.2 KiB
2.2 KiB
Node.js 安装指南
Windows 安装方法
1. 使用安装包
- 访问 Node.js 官网 https://nodejs.org/
- 下载 14.x LTS 版本安装包
- 运行安装包,按提示完成安装
- 验证安装:
node --version
npm --version
2. 使用 nvm-windows(推荐)
- 下载 nvm-windows:https://github.com/coreybutler/nvm-windows/releases
- 安装 nvm-windows
- 安装 Node.js:
nvm install 14.21.3
nvm use 14.21.3
Linux 安装方法
1. 使用 nvm(推荐)
# 安装 nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
# 重新加载配置
source ~/.bashrc
# 安装 Node.js 14
nvm install 14
nvm use 14
2. 使用包管理器
Ubuntu/Debian
# 添加 NodeSource 仓库
curl -fsSL https://deb.nodesource.com/setup_14.x | sudo -E bash -
# 安装 Node.js
sudo apt-get install -y nodejs
CentOS/RHEL
# 添加 NodeSource 仓库
curl -fsSL https://rpm.nodesource.com/setup_14.x | sudo bash -
# 安装 Node.js
sudo yum install -y nodejs
macOS 安装方法
1. 使用 Homebrew(推荐)
# 安装 Homebrew(如果未安装)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# 安装 Node.js 14
brew install node@14
# 添加环境变量
echo 'export PATH="/usr/local/opt/node@14/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc
2. 使用 nvm
# 安装 nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
# 重新加载配置
source ~/.zshrc
# 安装 Node.js 14
nvm install 14
nvm use 14
验证安装
安装完成后,运行以下命令验证:
# 检查 Node.js 版本
node --version # 应显示 v14.x.x
# 检查 npm 版本
npm --version # 应显示 6.x.x 或更高
常见问题
1. 权限问题
如果遇到权限错误,可以:
# Linux/macOS
sudo chown -R $USER /usr/local/lib/node_modules
2. 版本切换
如果需要在不同版本间切换:
# 使用 nvm
nvm list # 查看已安装版本
nvm use 14 # 切换到 14.x 版本
3. npm 配置
建议配置国内镜像源:
# 使用淘宝镜像
npm config set registry https://registry.npmmirror.com