Appearance
npm 安装与配置
本章将详细介绍npm的安装方法、配置选项和环境设置,帮助您正确设置npm开发环境。
安装Node.js和npm
npm通常与Node.js一起安装,有多种安装方式:
官方安装包
- 访问Node.js官网
- 下载适合您操作系统的安装包
- 运行安装程序并按照提示完成安装
包管理器安装
Windows (使用Chocolatey)
bash
choco install nodejs
macOS (使用Homebrew)
bash
# 安装Homebrew(如果未安装)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# 安装Node.js(包含npm)
brew install node
Ubuntu/Debian
bash
# 使用apt安装
sudo apt update
sudo apt install nodejs npm
# 或使用NodeSource仓库安装最新版本
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt-get install -y nodejs
CentOS/RHEL/Fedora
bash
# 使用dnf安装
sudo dnf install nodejs npm
# 或使用NodeSource仓库
curl -fsSL https://rpm.nodesource.com/setup_lts.x | sudo bash -
sudo dnf install nodejs
版本管理工具
nvm (Node Version Manager)
nvm允许您在同一台机器上管理多个Node.js版本:
bash
# 安装nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
# 重启终端或运行
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
# 安装最新LTS版本的Node.js
nvm install --lts
nvm use --lts
# 查看已安装版本
nvm list
fnm (Fast Node Manager) - 更快的替代方案
bash
# 安装fnm
curl -fsSL https://fnm.vercel.app/install | bash
# 配置shell(添加到.bashrc或.zshrc)
eval "$(fnm env --use-on-cd)"
# 安装和使用Node.js版本
fnm install --lts
fnm use --lts
验证安装
安装完成后,验证Node.js和npm是否正确安装:
bash
# 检查Node.js版本
node --version
# 检查npm版本
npm --version
# 检查安装路径
which node
which npm
npm配置文件
npm使用多层配置系统,配置文件按优先级排序:
配置文件层次
- 项目级:
项目根目录/.npmrc - 用户级:
~/.npmrc - 全局级:
$PREFIX/etc/npmrc - npm内置:
path/to/npm/npmrc
查看配置
bash
# 查看所有配置
npm config list
# 查看特定配置项
npm config get registry
npm config get cache
# 查看配置文件位置
npm config get userconfig # 用户配置
npm config get globalconfig # 全局配置
npm config get builtin # 内置配置
常用配置项详解
基础配置
bash
# 设置默认作者信息
npm config set init-author-name "Your Name"
npm config set init-author-email "your.email@example.com"
npm config set init-license "MIT"
# 设置默认仓库
npm config set init-version "1.0.0"
# 设置默认描述
npm config set save-exact true # 保存精确版本号
注册表配置
bash
# 设置npm注册表(默认)
npm config set registry "https://registry.npmjs.org/"
# 设置淘宝镜像(中国用户)
npm config set registry "https://registry.npmmirror.com/"
# 设置企业注册表
npm config set registry "https://your-enterprise-registry.com/"
npm config set "@your-scope:registry" "https://your-enterprise-registry.com/"
缓存配置
bash
# 设置缓存目录
npm config set cache "/path/to/cache"
# 设置缓存超时时间(毫秒)
npm config set cache-max 3600000
# 设置缓存最小时间(毫秒)
npm config set cache-min 600000
网络配置
bash
# 设置代理(如果在企业网络中)
npm config set proxy http://proxy.company.com:8080
npm config set https-proxy http://proxy.company.com:8080
# 设置超时时间
npm config set timeout 60000
# 设置并发连接数
npm config set maxsockets 50
配置文件示例
用户级配置文件 (~/.npmrc)
# 默认作者信息
init-author-name=Your Name
init-author-email=your.email@example.com
init-license=MIT
# 保存精确版本
save-exact=true
# 注册表设置
registry=https://registry.npmjs.org/
# 缓存设置
cache-max=3600000
cache-min=600000
# 安全设置
audit=true
audit-level=moderate
# 网络设置(如果需要)
# proxy=http://proxy.company.com:8080
# https-proxy=http://proxy.company.com:8080
项目级配置文件 (项目根目录/.npmrc)
# 项目特定设置
engine-strict=true
save-exact=true
# 如果使用企业注册表
@your-scope:registry=https://your-enterprise-registry.com/
全局包管理
全局包安装位置
bash
# 查看全局包安装位置
npm config get prefix
# 查看全局包列表
npm list -g --depth=0
# 查看全局包详细信息
npm list -g
配置全局包路径
bash
# 创建自定义全局包目录
mkdir ~/.npm-global
# 配置npm使用新目录
npm config set prefix '~/.npm-global'
# 将npm bin目录添加到PATH
# 在 ~/.bashrc 或 ~/.zshrc 中添加:
export PATH=~/.npm-global/bin:$PATH
# 重新加载配置
source ~/.bashrc
镜像源配置
使用淘宝镜像
bash
# 临时使用
npm install --registry https://registry.npmmirror.com/
# 永久设置
npm config set registry https://registry.npmmirror.com/
# 查看当前registry
npm config get registry
# 恢复默认registry
npm config set registry https://registry.npmjs.org/
使用nrm管理镜像源
bash
# 安装nrm
npm install -g nrm
# 查看可用镜像源
nrm ls
# 切换镜像源
nrm use taobao
# 测试镜像源速度
nrm test
安全配置
二因子认证(2FA)
bash
# 为npm账户启用2FA
npm profile enable-2fa auth-and-writes
审计配置
bash
# 启用自动安全审计
npm config set audit true
# 设置审计级别
npm config set audit-level moderate # low, moderate, high, critical
# 禁用审计(不推荐)
npm config set audit false
性能优化配置
并行安装配置
bash
# 设置最大并发数
npm config set maxsockets 50
# 设置重试次数
npm config set fetch-retry-mintimeout 10000
npm config set fetch-retry-maxtimeout 60000
npm config set fetch-retries 3
缓存优化
bash
# 定期清理缓存
npm cache verify
# 完全清理缓存(谨慎使用)
npm cache clean --force
# 设置缓存大小限制
npm config set cache-max 1073741824 # 1GB
环境变量
npm可以使用以下环境变量:
bash
# 在.bashrc或.zshrc中设置
export NPM_CONFIG_REGISTRY="https://registry.npmjs.org/"
export NPM_CONFIG_CACHE="/path/to/cache"
export NPM_CONFIG_PREFIX="/path/to/prefix"
export NPM_CONFIG_AUDIT=true
export NPM_CONFIG_FUND=false # 禁用funding消息
故障排除
常见权限问题
bash
# 如果遇到权限错误,可以:
# 1. 使用nvm安装Node.js(推荐)
# 2. 配置全局包路径
# 3. 使用sudo(不推荐)
sudo npm install -g package-name
网络问题解决
bash
# 检查网络连接
npm ping
# 如果使用代理,确保正确配置
npm config set proxy http://proxy.company.com:8080
npm config set https-proxy http://proxy.company.com:8080
# 禁用SSL验证(仅在必要时)
npm config set strict-ssl false # 不推荐在生产环境使用
通过正确配置npm,您可以获得更好的性能、安全性和使用体验。