跳到主要内容

Node.js 环境配置

本章将介绍如何安装和配置 Node.js 开发环境。

安装 Node.js

方式一:官网下载(推荐新手)

  1. 访问 Node.js 官网
  2. 下载 LTS(长期支持)版本
  3. 运行安装程序,按提示完成安装

方式二:使用包管理器

Windows (使用 winget)

# 安装 LTS 版本
winget install OpenJS.NodeJS.LTS

# 安装最新版本
winget install OpenJS.NodeJS

macOS (使用 Homebrew)

# 安装 Node.js
brew install node

# 安装指定版本
brew install node@20

Linux (Ubuntu/Debian)

# 使用 NodeSource 仓库
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt-get install -y nodejs

Linux (CentOS/RHEL)

# 使用 NodeSource 仓库
curl -fsSL https://rpm.nodesource.com/setup_20.x | sudo bash -
sudo yum install -y nodejs

方式三:使用 nvm(推荐开发者)

nvm(Node Version Manager)可以管理多个 Node.js 版本。

安装 nvm

Linux/macOS:

# 安装 nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

# 重新加载配置
source ~/.bashrc # 或 source ~/.zshrc

Windows:

使用 nvm-windows,下载并安装。

使用 nvm

# 查看可安装的版本
nvm ls-remote

# 安装指定版本
nvm install 20

# 安装最新 LTS 版本
nvm install --lts

# 切换版本
nvm use 20

# 查看已安装版本
nvm ls

# 设置默认版本
nvm alias default 20

# 查看当前版本
nvm current

验证安装

安装完成后,打开终端验证:

# 查看 Node.js 版本
node -v
# 输出:v20.11.0

# 查看 npm 版本
npm -v
# 输出:10.2.4

npm 配置

npm(Node Package Manager)是 Node.js 的包管理器。

配置镜像源

由于网络原因,国内用户建议配置镜像源:

# 查看当前镜像源
npm config get registry

# 设置淘宝镜像源
npm config set registry https://registry.npmmirror.com

# 恢复官方镜像源
npm config set registry https://registry.npmjs.org

使用 nrm 管理镜像源

# 安装 nrm
npm install -g nrm

# 列出可用镜像源
nrm ls

# 切换镜像源
nrm use taobao

# 测试镜像源速度
nrm test

其他配置

# 查看所有配置
npm config list

# 设置全局安装目录
npm config set prefix "D:\nodejs\npm_global"

# 设置缓存目录
npm config set cache "D:\nodejs\npm_cache"

# 设置代理
npm config set proxy http://proxy.example.com:8080
npm config set https-proxy http://proxy.example.com:8080

# 删除代理
npm config delete proxy
npm config delete https-proxy

开发工具

代码编辑器

Visual Studio Code(推荐)

  1. 下载安装 VS Code
  2. 安装 Node.js 扩展:
    • ESLint:代码检查
    • Prettier:代码格式化
    • Node.js Tools:Node.js 开发工具
    • npm Intellisense:npm 包自动补全

运行 Node.js 代码

交互式 REPL

node
# 进入 REPL 环境
> 1 + 1
2
> console.log("Hello")
Hello
undefined
> .exit # 退出

运行脚本文件

# 创建脚本文件
echo "console.log('Hello, Node.js!')" > hello.js

# 运行脚本
node hello.js
# 输出:Hello, Node.js!

使用 --watch 模式(Node.js 18+)

# 文件变化时自动重新运行
node --watch hello.js

package.json 初始化

# 创建 package.json
npm init

# 快速创建(使用默认值)
npm init -y

package.json 示例:

{
"name": "my-project",
"version": "1.0.0",
"description": "My Node.js project",
"main": "index.js",
"scripts": {
"start": "node index.js",
"dev": "node --watch index.js",
"test": "node test.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {},
"devDependencies": {}
}

字段说明:

字段说明
name项目名称,必须是小写字母、数字、连字符
version版本号,遵循语义化版本规范
main程序入口文件
scriptsnpm 脚本命令
dependencies生产环境依赖
devDependencies开发环境依赖

安装依赖包

# 安装包(添加到 dependencies)
npm install express
npm install [email protected] # 安装指定版本

# 安装包(添加到 devDependencies)
npm install --save-dev typescript
npm install -D typescript

# 全局安装
npm install -g nodemon

# 安装 package.json 中的所有依赖
npm install

# 安装生产环境依赖(排除 devDependencies)
npm install --production

常用全局工具

# nodemon - 文件变化自动重启
npm install -g nodemon
nodemon app.js

# pm2 - 进程管理
npm install -g pm2
pm2 start app.js

# npx - 执行 npm 包命令(Node.js 自带)
npx create-react-app my-app

常见问题

1. 权限问题(Linux/macOS)

# 避免使用 sudo 安装全局包
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'

# 添加到 PATH
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
source ~/.bashrc

2. 清除 npm 缓存

npm cache clean --force

3. 重新安装依赖

# 删除 node_modules 和 package-lock.json
rm -rf node_modules package-lock.json

# 重新安装
npm install

4. 查看已安装的包

# 查看当前项目安装的包
npm list

# 查看全局安装的包
npm list -g --depth=0

# 查看过期的包
npm outdated

5. 更新 Node.js

使用 nvm:

# 安装新版本
nvm install 20

# 切换到新版本
nvm use 20

# 迁移全局包
nvm reinstall-packages 18

小结

本章我们学习了:

  1. Node.js 的多种安装方式
  2. 使用 nvm 管理多个 Node.js 版本
  3. npm 的配置和使用
  4. 开发环境的搭建
  5. package.json 的创建和配置
  6. 常见问题的解决方法

练习

  1. 安装 Node.js 并验证安装成功
  2. 使用 nvm 安装并切换不同版本的 Node.js
  3. 创建一个项目并初始化 package.json
  4. 安装一个依赖包并在代码中使用
  5. 配置 npm 镜像源