pnpm 的 .npmrc 配置文件提供了丰富的选项来自定义包管理行为。
基础配置:
ini# .npmrc # 注册表配置 registry=https://registry.npmjs.org/ # 淘宝镜像(国内加速) registry=https://registry.npmmirror.com/ # 作用域包注册表 @mycompany:registry=https://npm.mycompany.com/
存储配置:
ini# 全局 store 位置 store-dir=/path/to/custom/store # 缓存目录 cache-dir=/path/to/custom/cache # 状态目录 state-dir=/path/to/custom/state
安装行为配置:
ini# 严格模式 strict-peer-dependencies=true # 自动安装 peer dependencies auto-install-peers=true # 扁平化模式(兼容 npm) shamefully-hoist=true # 只安装生产依赖 production=true # 忽略 engines 检查 engine-strict=false
网络配置:
ini# 并发下载数 network-concurrency=16 # 请求超时时间(毫秒) fetch-timeout=60000 # 重试次数 fetch-retries=3 # 代理设置 proxy=http://proxy.company.com:8080 https-proxy=http://proxy.company.com:8080 # 不使用代理的域名 no-proxy=localhost,127.0.0.1
Workspace 配置:
ini# 递归安装时链接 workspace 包 link-workspace-packages=true # 优先使用 workspace 版本 prefer-workspace-packages=true # 保存 workspace 协议 save-workspace-protocol=true
安全与审计:
ini# 忽略审计警告 ignore-workspace-root-check=true # 允许执行 postinstall 脚本 ignore-scripts=false # 只读模式(不修改 lock 文件) frozen-lockfile=true
输出配置:
ini# 日志级别 loglevel=info # 不显示进度条 reporter=silent # 使用颜色输出 color=always
常用配置组合:
ini# 开发环境配置 strict-peer-dependencies=false auto-install-peers=true shamefully-hoist=false # CI/CD 配置 frozen-lockfile=true prefer-offline=true reporter=silent # Monorepo 配置 link-workspace-packages=true prefer-workspace-packages=true
环境变量配置:
bash# 通过环境变量设置 export npm_config_registry=https://registry.npmmirror.com/ export npm_config_store_dir=/custom/store
查看当前配置:
bash# 查看所有配置 pnpm config list # 查看特定配置 pnpm config get registry # 设置配置 pnpm config set registry https://registry.npmmirror.com/ # 删除配置 pnpm config delete registry
配置优先级:
- 命令行参数
- 环境变量
- 项目
.npmrc - 用户
~/.npmrc - 全局
/etc/npmrc - 默认值
实际应用示例:
ini# 企业环境配置 registry=https://npm.company.com/ @company:registry=https://npm.company.com/ # 使用内部代理 proxy=http://proxy.company.com:8080 https-proxy=http://proxy.company.com:8080 # 严格模式 strict-peer-dependencies=true engine-strict=true # 性能优化 network-concurrency=32 fetch-retries=5