Node.js 环境配置
本章节将指导你配置 Node.js 开发环境,包括 Node.js 安装、包管理器配置、开发工具等。
📋 配置清单
- Node.js 安装 (fnm)
- 包管理器配置 (pnpm + bun)
- 全局工具安装
- 项目配置
- 验证安装
1. 安装 Node.js
使用 fnm 安装 (推荐)
macOS/Linux
bash
# 安装 fnm
curl -fsSL https://fnm.vercel.app/install | bashbash
# 添加到 shell 配置
echo 'eval "$(fnm env --use-on-cd)"' >> ~/.zshrcbash
# 重新加载配置
source ~/.zshrcbash
# 安装 LTS 版本
fnm install --ltsbash
# 使用 LTS 版本
fnm use lts-latestbash
# 设置为默认版本
fnm default lts-latestWindows
powershell
# 使用 Chocolatey 安装 fnm
choco install fnmpowershell
# 安装 LTS 版本
fnm install --ltspowershell
# 使用 LTS 版本
fnm use lts-latestpowershell
# 设置为默认版本
fnm default lts-latest验证安装
macOS/Linux
bash
# 检查 fnm
fnm --versionbash
# 检查 Node.js
node --versionbash
# 检查 npm
npm --versionWindows
powershell
# 检查 fnm
fnm --versionpowershell
# 检查 Node.js
node --versionpowershell
# 检查 npm
npm --version2. 配置包管理器
启用 Corepack (管理 pnpm)
macOS/Linux
bash
# 启用 Corepack
corepack enablebash
# 准备 pnpm
corepack prepare pnpm@latest --activatebash
# 配置 pnpm 环境
pnpm setupWindows
powershell
# 启用 Corepack
corepack enablepowershell
# 准备 pnpm
corepack prepare pnpm@latest --activatepowershell
# 配置 pnpm 环境
pnpm setup安装 Bun
macOS/Linux
bash
# 安装 Bun
curl -fsSL https://bun.sh/install | bashbash
# 重新加载配置
source ~/.zshrcWindows
powershell
# 安装 Bun
# 访问: https://bun.sh/
# 下载并安装 Windows 版本验证包管理器
macOS/Linux
bash
# 检查 pnpm
pnpm --versionbash
# 检查 bun
bun --versionWindows
powershell
# 检查 pnpm
pnpm --versionpowershell
# 检查 bun
bun --version3. 安装全局工具
安装 nrm (npm 镜像管理)
macOS/Linux
bash
# 安装 nrm
pnpm add -g nrmbash
# 查看可用镜像
nrm lsbash
# 切换到淘宝镜像
nrm use taobaoWindows
powershell
# 安装 nrm
pnpm add -g nrmpowershell
# 查看可用镜像
nrm lspowershell
# 切换到淘宝镜像
nrm use taobao安装 Whistle (网络代理调试)
macOS/Linux
bash
# 安装 Whistle
pnpm add -g whistlebash
# 启动 Whistle
w2 startbash
# 查看 Whistle 状态
w2 statusWindows
powershell
# 安装 Whistle
pnpm add -g whistlepowershell
# 启动 Whistle
w2 startpowershell
# 查看 Whistle 状态
w2 status4. 项目配置
创建新项目
使用 pnpm
macOS/Linux
bash
# 创建项目目录
mkdir myproject
cd myprojectbash
# 初始化项目
pnpm initWindows
powershell
# 创建项目目录
mkdir myproject
cd myprojectpowershell
# 初始化项目
pnpm init使用 Bun
macOS/Linux
bash
# 创建项目目录
mkdir myproject
cd myprojectbash
# 初始化项目
bun initWindows
powershell
# 创建项目目录
mkdir myproject
cd myprojectpowershell
# 初始化项目
bun init安装依赖
使用 pnpm
macOS/Linux
bash
# 安装生产依赖
pnpm add expressbash
# 安装开发依赖
pnpm add -D typescriptbash
# 安装所有依赖
pnpm installWindows
powershell
# 安装生产依赖
pnpm add expresspowershell
# 安装开发依赖
pnpm add -D typescriptpowershell
# 安装所有依赖
pnpm install使用 Bun
macOS/Linux
bash
# 安装生产依赖
bun add expressbash
# 安装开发依赖
bun add -d typescriptbash
# 安装所有依赖
bun installWindows
powershell
# 安装生产依赖
bun add expresspowershell
# 安装开发依赖
bun add -d typescriptpowershell
# 安装所有依赖
bun install5. 环境变量配置
创建 .env 文件
macOS/Linux
bash
# 创建 .env 文件
touch .envbash
# 创建 .env.example 文件
touch .env.exampleWindows
powershell
# 创建 .env 文件
New-Item -Path .env -ItemType Filepowershell
# 创建 .env.example 文件
New-Item -Path .env.example -ItemType File安装 dotenv
使用 pnpm
bash
pnpm add dotenv使用 Bun
bash
bun add dotenv6. 调试配置
VS Code 调试配置
创建 .vscode/launch.json 文件:
json
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/index.js",
"envFile": "${workspaceFolder}/.env"
},
{
"type": "node",
"request": "launch",
"name": "Launch with Bun",
"program": "${workspaceFolder}/index.js",
"runtimeExecutable": "bun",
"envFile": "${workspaceFolder}/.env"
}
]
}7. 性能优化
配置 pnpm 缓存
macOS/Linux
bash
# 查看 pnpm 缓存位置
pnpm store pathbash
# 清理 pnpm 缓存
pnpm store pruneWindows
powershell
# 查看 pnpm 缓存位置
pnpm store pathpowershell
# 清理 pnpm 缓存
pnpm store prune配置 Bun 缓存
macOS/Linux
bash
# 查看 Bun 缓存位置
bun pm cachebash
# 清理 Bun 缓存
bun pm cache rmWindows
powershell
# 查看 Bun 缓存位置
bun pm cachepowershell
# 清理 Bun 缓存
bun pm cache rm✅ 验证安装
完成安装后,验证以下工具是否正常工作:
macOS/Linux
bash
# 检查 Node.js
node --versionbash
# 检查 fnm
fnm --versionbash
# 检查 pnpm
pnpm --versionbash
# 检查 bun
bun --versionbash
# 检查 nrm
nrm --versionbash
# 检查 whistle
w2 --versionWindows
powershell
# 检查 Node.js
node --versionpowershell
# 检查 fnm
fnm --versionpowershell
# 检查 pnpm
pnpm --versionpowershell
# 检查 bun
bun --versionpowershell
# 检查 nrm
nrm --versionpowershell
# 检查 whistle
w2 --version🎉 下一步
Node.js 环境配置完成后,继续配置其他编程语言环境:
遇到问题? 查看 问题排查 页面。