安装 Node.js
本指南将详细说明如何在 Windows、macOS 和 Linux 操作系统上安装 Node.js 运行时环境。Node.js 是 PCode 运行所需的核心依赖。
安装 Node.js
Section titled “安装 Node.js”方法一:官方安装包(推荐)
Section titled “方法一:官方安装包(推荐)”- 访问 Node.js 官方网站
- 下载 **LTS(长期支持)**版本的 Windows 安装程序(
.msi文件) - 双击运行安装程序
- 按照安装向导的提示完成安装:
- 接受许可协议
- 选择安装路径(默认即可)
- 确保勾选 “Automatically install the necessary tools”(自动安装必要的工具)
- 点击 “Install” 开始安装
- 安装完成后,点击 “Finish”
方法二:使用 winget
Section titled “方法二:使用 winget”打开 PowerShell 或命令提示符,运行以下命令:
winget install OpenJS.NodeJS.LTS安装完成后,重新打开终端以使更改生效。
方法一:官方安装包(推荐)
Section titled “方法一:官方安装包(推荐)”- 访问 Node.js 官方网站
- 下载 **LTS(长期支持)**版本的 macOS 安装程序(
.pkg文件) - 双击运行安装程序
- 按照安装向导的提示完成安装:
- 点击 “继续”
- 接受许可协议
- 选择安装磁盘
- 点击 “安装” 并输入管理员密码
- 安装完成后,点击 “关闭”
方法二:使用 Homebrew
Section titled “方法二:使用 Homebrew”如果您已经安装了 Homebrew,可以通过终端安装 Node.js:
brew install node使用 NodeSource 仓库(推荐)
Section titled “使用 NodeSource 仓库(推荐)”这是获取最新 Node.js 版本的最佳方式:
# 使用 curl 下载安装脚本curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -sudo apt-get install -y nodejs使用 apt 包管理器
Section titled “使用 apt 包管理器”如果您不需要最新版本,可以使用 Ubuntu 自带的软件仓库:
sudo apt-get updatesudo apt-get install -y nodejs npm使用 NodeSource 仓库(推荐)
Section titled “使用 NodeSource 仓库(推荐)”# 使用 curl 下载安装脚本curl -fsSL https://rpm.nodesource.com/setup_lts.x | sudo bash -sudo yum install -y nodejs使用 yum 包管理器
Section titled “使用 yum 包管理器”sudo yum install -y nodejs npm使用 dnf 包管理器
Section titled “使用 dnf 包管理器”sudo dnf install -y nodejs npm使用版本管理工具
Section titled “使用版本管理工具”如果您需要在多个 Node.js 版本之间切换,可以使用版本管理工具。
nvm (Node Version Manager)
Section titled “nvm (Node Version Manager)”Linux/macOS
Section titled “Linux/macOS”# 使用 curl 安装curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
# 或使用 wget 安装wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash安装完成后,重新加载您的 shell 配置:
source ~/.bashrc# 或对于 Zsh 用户source ~/.zshrcWindows
Section titled “Windows”在 Windows 上,推荐使用 nvm-windows:
- 从 nvm-windows releases 下载最新的
nvm-setup.exe - 运行安装程序
- 重新打开命令提示符或 PowerShell
安装 nvm 后,您可以使用它来安装和管理 Node.js 版本:
# 安装最新的 LTS 版本(推荐)nvm install --lts
# 安装 Node.js 24(推荐版本)nvm install 24
# 安装特定的 Node.js 版本nvm install 18.20.0
# 列出已安装的版本nvm list
# 切换到指定的 Node.js 版本nvm use 24
# 设置默认版本nvm alias default 24
# Windows nvm-windows 语法略有不同nvm install 24nvm use 24fnm (Fast Node Manager)
Section titled “fnm (Fast Node Manager)”fnm 是一个更快、更简单的 Node.js 版本管理工具,使用 Rust 构建。
Linux/macOS
Section titled “Linux/macOS”# 使用 curl 安装curl -fsSL https://fnm.vercel.app/install | bash
# 安装完成后,重新加载 shell 配置source ~/.bashrc# 或对于 Zsh 用户source ~/.zshrcWindows
Section titled “Windows”# 使用 scoopscoop install fnm
# 或使用 wingetwinget install Schniz.fnm# 安装最新的 LTS 版本(推荐)fnm install --lts
# 安装 Node.js 24(推荐版本)fnm install 24
# 安装特定的 Node.js 版本fnm install 18.20.0
# 列出已安装的版本fnm list
# 切换到指定的 Node.js 版本fnm use 24
# 设置默认版本fnm default 24中国大陆用户加速
Section titled “中国大陆用户加速”如果您在中国大陆地区,下载 Node.js 和 npm 包可能会遇到速度慢的问题。以下是一些加速方案。
使用国内镜像源
Section titled “使用国内镜像源”npm 淘宝镜像(推荐)
Section titled “npm 淘宝镜像(推荐)”# 临时使用淘宝镜像npm install --registry=https://registry.npmmirror.com
# 永久设置淘宝镜像npm config set registry https://registry.npmmirror.com
# 验证镜像是否设置成功npm config get registry使用 nrm 管理镜像源
Section titled “使用 nrm 管理镜像源”nrm (npm registry manager) 可以帮助您快速切换 npm 镜像源:
# 安装 nrmnpm install -g nrm
# 列出可用的镜像源nrm ls
# 切换到淘宝镜像nrm use taobao
# 测试镜像源速度nrm test下载 Node.js 安装包加速
Section titled “下载 Node.js 安装包加速”如果从 GitHub 下载 nvm 或其他工具速度慢,可以使用 FastGit 镜像:
# 使用 FastGit 加速 nvm 安装curl -o- https://fastgit.org/nvm-sh/nvm/raw/master/install.sh | bashHomebrew 国内镜像
Section titled “Homebrew 国内镜像”如果您在中国大陆使用 Homebrew,可以配置国内镜像加速:
# 安装 Homebrew 时使用国内镜像/bin/bash -c "$(curl -fsSL https://gitee.com/ineo6/homebrew-install/raw/master/install.sh)"
# 或使用中科大的镜像export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles中国大陆特有包管理器
Section titled “中国大陆特有包管理器”cnpm 是淘宝团队提供的 npm 客户端,默认使用淘宝镜像:
# 安装 cnpmnpm install -g cnpm --registry=https://registry.npmmirror.com
# 使用 cnpm 安装包cnpm install <package-name>验证镜像加速效果
Section titled “验证镜像加速效果”设置镜像后,可以通过以下命令验证:
# 测试下载速度time npm install express --registry=https://registry.npmmirror.com
# 对比官方源速度time npm install express --registry=https://registry.npmjs.org安装完成后,请验证 Node.js 和 npm 是否正确安装:
# 检查 Node.js 版本node --version
# 检查 npm 版本npm --version验证命令输出示例
Section titled “验证命令输出示例”成功的验证输出应该类似:
$ node --versionv24.3.0
$ npm --version10.2.3Windows 问题
Section titled “Windows 问题”如果在安装后 node 或 npm 命令不可用:
- 重新启动命令提示符或 PowerShell
- 如果仍然不可用,手动将 Node.js 添加到 PATH:
- 搜索 “环境变量” 并打开 “编辑系统环境变量”
- 点击 “环境变量”
- 在 “系统变量” 中找到 “Path”,点击 “编辑”
- 添加 Node.js 的安装路径(默认:
C:\Program Files\nodejs\) - 点击 “确定” 保存更改
如果在使用 nvm 时遇到执行策略错误:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUsermacOS 问题
Section titled “macOS 问题”如果在安装全局 npm 包时遇到权限错误:
# 修复 npm 权限sudo chown -R $(whoami) ~/.npmsudo chown -R $(whoami) /usr/local/lib/node_modules如果安装后命令不可用,请重新启动终端或手动添加到 PATH(通常在 /usr/local/bin)。
Linux 问题
Section titled “Linux 问题”如果在安装全局 npm 包时遇到权限错误,建议使用 Node.js 版本管理工具(如 nvm 或 fnm)而不是系统包管理器。
如果通过系统包管理器安装的 Node.js 版本过旧:
- 使用 NodeSource 仓库(推荐,见上文)
- 或使用版本管理工具(nvm/fnm)
如果验证失败,检查以下内容:
# 检查 Node.js 是否在 PATH 中where node
# 检查安装路径Get-Command node# 检查 Node.js 是否在 PATH 中which node
# 检查安装路径ls -l $(which node)# 检查 Node.js 是否在 PATH 中which node
# 检查安装路径ls -l $(which node)
# 检查版本详细信息node -vNode.js 安装完成后,返回安装指南继续配置。
需要更多帮助?
Section titled “需要更多帮助?”如果您遇到此处未涵盖的问题:
- 访问 Node.js 官方文档
- 查看 GitHub Issues 寻找类似问题
- 访问我们的社区论坛寻求帮助