Node.js 安装配置
本章介绍如何在不同操作系统上安装 Node.js,以及如何配置开发环境。
安装方式
官方安装包
从 Node.js 官网下载对应操作系统的安装包:
- LTS(长期支持版):推荐生产环境使用,稳定可靠
- Current(当前版):包含最新特性,适合尝鲜和测试
访问 Node.js 官网 下载安装。
Windows 安装
- 下载
.msi安装包 - 双击运行安装向导
- 勾选 "Automatically install the necessary tools"(自动安装必要工具)
- 完成安装后,打开命令提示符验证:
node --version
npm --version
macOS 安装
使用官方安装包
- 下载
.pkg安装包 - 双击运行安装向导
- 完成后验证安装
使用 Homebrew
# 安装 Node.js
brew install node
# 安装指定版本
brew install node@20
# 验证安装
node --version
npm --version
Linux 安装
Ubuntu/Debian
# 使用 NodeSource 仓库
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt-get install -y nodejs
# 验证安装
node --version
npm --version
CentOS/RHEL
# 使用 NodeSource 仓库
curl -fsSL https://rpm.nodesource.com/setup_20.x | sudo bash -
sudo yum install -y nodejs
# 验证安装
node --version
npm --version
版本管理工具
当需要在多个项目间切换不同 Node.js 版本时,推荐使用版本管理工具。
nvm(Node Version Manager)
安装 nvm
macOS/Linux:
# 使用 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
# 重新加载配置
source ~/.bashrc
# 或
source ~/.zshrc
Windows:
使用 nvm-windows,下载 .exe 安装包安装。
使用 nvm
# 查看可安装的 Node.js 版本
nvm ls-remote
# 安装指定版本
nvm install 20.10.0
# 安装最新 LTS 版本
nvm install --lts
# 切换版本
nvm use 20.10.0
# 查看已安装版本
nvm ls
# 设置默认版本
nvm alias default 20.10.0
# 在项目根目录创建 .nvmrc 文件
echo "20.10.0" > .nvmrc
nvm use # 自动读取 .nvmrc
fnm(Fast Node Manager)
fnm 使用 Rust 编写,速度更快:
# 安装 fnm
# macOS/Linux
curl -fsSL https://fnm.vercel.app/install | bash
# Windows (使用 winget)
winget install Schniz.fnm
# 使用 fnm
fnm install 20
fnm use 20
fnm default 20
包管理器配置
npm 配置
# 查看 npm 配置
npm config list
# 设置镜像源(国内加速)
npm config set registry https://registry.npmmirror.com
# 恢复官方源
npm config set registry https://registry.npmjs.org
# 设置缓存目录
npm config set cache ~/.npm-cache
# 设置代理(如需要)
npm config set proxy http://proxy-server:port
npm config set https-proxy http://proxy-server:port
pnpm 安装
pnpm 是更高效的包管理器:
# 使用 npm 安装
npm install -g pnpm
# 设置镜像源
pnpm config set registry https://registry.npmmirror.com
# 常用命令
pnpm install # 安装依赖
pnpm add package-name # 添加依赖
pnpm add -D package-name # 添加开发依赖
yarn 安装
# 使用 npm 安装
npm install -g yarn
# 设置镜像源
yarn config set registry https://registry.npmmirror.com
# 常用命令
yarn install # 安装依赖
yarn add package-name # 添加依赖
yarn add -D package-name # 添加开发依赖
开发工具配置
VS Code 扩展
推荐安装以下扩展:
| 扩展名称 | 用途 |
|---|---|
| ES7+ React/Redux/React-Native snippets | 代码片段 |
| ESLint | 代码检查 |
| Prettier | 代码格式化 |
| Node.js Tools | Node.js 开发工具 |
| npm Intellisense | npm 包自动补全 |
| Path Intellisense | 路径自动补全 |
代码检查配置
创建 .eslintrc.json:
{
"env": {
"node": true,
"es2021": true
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"rules": {
"no-unused-vars": "warn",
"no-console": "off"
}
}
代码格式化配置
创建 .prettierrc:
{
"semi": true,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "es5",
"printWidth": 80
}
项目初始化
创建新项目
# 创建项目目录
mkdir my-nodejs-project
cd my-nodejs-project
# 初始化 package.json
npm init
# 或使用默认配置
npm init -y
package.json 详解
{
"name": "my-nodejs-project",
"version": "1.0.0",
"description": "项目描述",
"main": "index.js",
"scripts": {
"start": "node index.js",
"dev": "node --watch index.js",
"test": "node --test"
},
"keywords": ["nodejs", "tutorial"],
"author": "Your Name",
"license": "MIT",
"dependencies": {
"express": "^4.18.2"
},
"devDependencies": {
"eslint": "^8.50.0"
},
"engines": {
"node": ">=18.0.0"
}
}
常用 scripts
{
"scripts": {
"start": "node index.js",
"dev": "node --watch index.js",
"dev:inspect": "node --inspect --watch index.js",
"test": "node --test",
"test:coverage": "node --test --experimental-test-coverage",
"lint": "eslint .",
"format": "prettier --write ."
}
}
环境变量
使用 .env 文件
# 安装 dotenv
npm install dotenv
创建 .env 文件:
# 数据库配置
DB_HOST=localhost
DB_PORT=5432
DB_NAME=myapp
DB_USER=admin
DB_PASSWORD=secret
# 应用配置
PORT=3000
NODE_ENV=development
在代码中使用:
// 方式1:在入口文件顶部加载
require('dotenv').config();
// 方式2:使用 ES Modules
import 'dotenv/config';
// 访问环境变量
console.log(process.env.DB_HOST);
console.log(process.env.PORT);
生产环境配置
# 设置 NODE_ENV
export NODE_ENV=production # Linux/macOS
set NODE_ENV=production # Windows
# 在代码中检查环境
if (process.env.NODE_ENV === 'production') {
// 生产环境配置
}
常见问题
1. 权限错误(EACCES)
# 方法1:修改 npm 默认目录
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
# 添加到 PATH
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
# 方法2:使用 nvm 管理版本(推荐)
# nvm 安装的 Node.js 不需要 sudo
2. 网络超时
# 设置超时时间
npm config set fetch-timeout 600000
# 使用国内镜像
npm config set registry https://registry.npmmirror.com
3. 清除缓存
# 清除 npm 缓存
npm cache clean --force
# 重新安装依赖
rm -rf node_modules package-lock.json
npm install
小结
本章我们学习了:
- 安装方式:官方安装包、包管理器、版本管理工具
- 版本管理:使用 nvm 管理多个 Node.js 版本
- 包管理器:npm、pnpm、yarn 的配置和使用
- 开发工具:VS Code 扩展和代码检查配置
- 项目初始化:package.json 配置和脚本
- 环境变量:使用 .env 文件管理配置
练习
- 使用 nvm 安装 Node.js LTS 版本
- 创建一个新的 Node.js 项目,配置 ESLint 和 Prettier
- 配置 npm 镜像源,安装一个常用包
- 创建 .env 文件并在代码中读取环境变量