npm 提供了强大的配置系统,允许开发者通过 .npmrc 文件和命令行选项自定义 npm 的行为。理解 npm 配置对于优化开发流程和解决常见问题非常重要。
.npmrc 文件
配置文件位置
npm 配置文件可以存在于多个位置,按优先级从高到低:
- 项目级:
项目根目录/.npmrc - 用户级:
~/.npmrc - 全局级:
$PREFIX/etc/npmrc - 内置级:npm 内置默认配置
项目级配置
在项目根目录创建 .npmrc 文件:
ini# 项目级 .npmrc registry=https://registry.npmmirror.com save-exact=true engine-strict=true
优势:
- 项目特定的配置
- 可以提交到版本控制
- 团队成员共享配置
用户级配置
位于用户主目录的 .npmrc 文件:
bash# 查看用户级配置文件位置 npm config get userconfig
常用配置:
ini# 用户级 .npmrc registry=https://registry.npmmirror.com prefix=/usr/local cache=/Users/username/.npm-cache
全局级配置
位于 npm 安装目录的配置文件:
bash# 查看全局配置文件位置 npm config get globalconfig
常用配置选项
Registry 配置
ini# 设置 registry registry=https://registry.npmjs.org # 使用淘宝镜像 registry=https://registry.npmmirror.com # 使用私有 registry registry=https://registry.yourcompany.com # 为特定 scope 设置 registry @yourcompany:registry=https://registry.yourcompany.com
认证配置
ini# 使用 token 认证 //registry.npmjs.org/:_authToken=YOUR_TOKEN # 使用用户名密码 //registry.yourcompany.com/:username=your-username //registry.yourcompany.com/:_password=YOUR_PASSWORD # 从环境变量读取 //registry.npmjs.org/:_authToken=${NPM_TOKEN}
缓存配置
ini# 设置缓存目录 cache=/path/to/cache # 设置缓存最大大小(字节) cache-max=10737418240 # 设置缓存最小保留时间(秒) cache-min=3600
安装配置
ini# 精确安装版本 save-exact=true # 保存前缀 save-prefix=^ # 保存开发依赖 save-dev=false # 保存可选依赖 save-optional=false # 保存同伴依赖 save-peer=false # 使用严格模式 engine-strict=true # 忽略脚本 ignore-scripts=false
网络配置
ini# 设置代理 https-proxy=http://proxy.example.com:8080 http-proxy=http://proxy.example.com:8080 # 设置超时时间(毫秒) fetch-timeout=60000 # 设置重试次数 fetch-retries=3 # 设置重试最小超时(毫秒) fetch-retry-mintimeout=10000 # 设置重试最大超时(毫秒) fetch-retry-maxtimeout=60000 # 设置最大并发连接数 maxsockets=50 # 设置网络请求并发数 network-concurrency=16
安全配置
ini# 启用严格 SSL strict-ssl=true # 启用审计 audit=true # 设置审计级别 audit-level=moderate # 设置 CA 证书 cafile=/path/to/ca.pem # 设置证书 cert=/path/to/cert.pem key=/path/to/key.pem
工作区配置
ini# 启用工作区 workspaces=true # 设置工作区目录 workspace=/path/to/workspace
其他配置
ini# 设置前缀 prefix=/usr/local # 设置日志级别 loglevel=info # 设置颜色输出 color=true # 设置进度条 progress=true # 设置 Unicode 字符 unicode=true # 设置包装器 shell=bash # 设置编辑器 editor=vim # 设置浏览器 browser=google-chrome
命令行配置
查看配置
bash# 查看所有配置 npm config list # 查看特定配置 npm config get registry # 查看用户配置 npm config list --user # 查看全局配置 npm config list --global # 查看项目配置 npm config list --project
设置配置
bash# 设置配置 npm config set registry https://registry.npmmirror.com # 设置全局配置 npm config set registry https://registry.npmmirror.com --global # 删除配置 npm config delete registry # 编辑配置文件 npm config edit
环境变量
npm 支持通过环境变量配置:
bash# 设置 registry export npm_config_registry=https://registry.npmmirror.com # 设置缓存目录 export npm_config_cache=/path/to/cache # 设置代理 export npm_config_https-proxy=http://proxy.example.com:8080 # 设置 token export npm_config__authToken=YOUR_TOKEN
高级配置
Scope 配置
为不同的 scope 设置不同的配置:
ini# 公开包使用官方 registry registry=https://registry.npmjs.org # 私有包使用私有 registry @yourcompany:registry=https://registry.yourcompany.com # 为特定 scope 设置认证 @yourcompany:registry=https://registry.yourcompany.com //registry.yourcompany.com/:_authToken=${YOURCOMPANY_TOKEN}
条件配置
根据条件选择不同的配置:
ini# 开发环境 $npm_config_env=development registry=https://registry.npmmirror.com # 生产环境 $npm_config_env=production registry=https://registry.npmjs.org
脚本配置
在 npm scripts 中使用配置:
json{ "scripts": { "install:dev": "npm install --registry=https://registry.npmmirror.com", "install:prod": "npm install --registry=https://registry.npmjs.org" } }
配置优先级
配置优先级从高到低:
- 命令行选项
- 环境变量
- 项目级
.npmrc - 用户级
.npmrc - 全局级
npmrc - npm 内置默认配置
示例:
bash# 命令行选项优先级最高 npm install --registry=https://custom-registry.com # 即使 .npmrc 中设置了其他 registry # 命令行选项会覆盖
最佳实践
1. 项目级配置
ini# 项目 .npmrc registry=https://registry.npmmirror.com save-exact=true engine-strict=true audit=true audit-level=moderate
2. 用户级配置
ini# 用户 .npmrc registry=https://registry.npmmirror.com cache=~/.npm-cache prefix=~/.npm-global loglevel=warn
3. CI/CD 配置
yaml# GitHub Actions - name: Configure npm run: | echo "registry=https://registry.npmjs.org" > .npmrc echo "//registry.npmjs.org/:_authToken=\${NPM_TOKEN}" >> .npmrc env: NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
4. 安全配置
ini# 安全配置 strict-ssl=true audit=true audit-level=high ignore-scripts=false
5. 性能配置
ini# 性能配置 cache=~/.npm-cache cache-max=10737418240 maxsockets=50 network-concurrency=16 fetch-retries=3
常见问题
1. 配置不生效
bash# 检查配置优先级 npm config list # 检查特定配置 npm config get registry # 清除缓存 npm cache clean --force
2. 认证失败
bash# 检查 token npm config get _authToken # 重新登录 npm login # 检查 registry npm config get registry
3. 网络问题
bash# 检查代理配置 npm config get https-proxy # 检查超时设置 npm config get fetch-timeout # 使用镜像 npm config set registry https://registry.npmmirror.com
4. 缓存问题
bash# 清理缓存 npm cache clean --force # 验证缓存 npm cache verify # 检查缓存位置 npm config get cache
配置示例
开发环境
ini# 开发环境 .npmrc registry=https://registry.npmmirror.com save-exact=false save-prefix=^ engine-strict=false audit=false loglevel=info
生产环境
ini# 生产环境 .npmrc registry=https://registry.npmjs.org save-exact=true engine-strict=true audit=true audit-level=high strict-ssl=true loglevel=warn
企业环境
ini# 企业环境 .npmrc registry=https://registry.yourcompany.com @yourcompany:registry=https://registry.yourcompany.com //registry.yourcompany.com/:_authToken=${NPM_TOKEN} strict-ssl=true audit=true audit-level=moderate
理解 npm 配置系统可以帮助开发者优化开发流程、解决常见问题,并确保团队使用一致的配置。