npm ERR!代码 ELIFECYCLE npm ERR!errno 1 npm ERR!audit:`npm audit` npm ERR!退出状态 1 npm ERR!发现 1 个高严重性漏洞
npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! audit: `npm audit` npm ERR! Exit status 1 npm ERR! Found 1 high severity vulnerability
ID: policy/npm-audit-fails-on-high-severity-vulnerability
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| npm 8.x | active | — | — | — |
| npm 9.x | active | — | — | — |
| npm 10.x | active | — | — | — |
根因分析
项目的 npm audit 策略(在 .npmrc 或 package.json 中配置)将高严重性漏洞视为构建失败,从而阻止 CI/CD 流水线。
English
The project's npm audit policy (configured in .npmrc or package.json) treats high-severity vulnerabilities as build failures, blocking CI/CD pipelines.
官方文档
https://docs.npmjs.com/cli/v10/commands/npm-audit解决方案
-
使用 'npm audit fix --force'(如果可接受破坏性更改)更新易受攻击的包到修补版本,或手动在 package.json 中升级依赖。 示例: npm audit fix --force # 或手动: npm install [email protected] --save-exact
-
在 .npmrc 中覆盖审计策略,临时允许高严重性漏洞,将 'audit-level=high' 设置为 'audit-level=critical'。这仅适用于开发环境,不适用于生产。 示例: echo "audit-level=critical" >> .npmrc npm audit
-
使用第三方漏洞管理工具如 Snyk 或 Dependabot 自动修复漏洞并更新 PR,绕过 npm audit 的严格退出代码。 示例: # 安装 Snyk CLI npm install -g snyk snyk test --severity-threshold=high
无效尝试
常见但无效的做法:
-
Running 'npm audit fix' blindly
35% 失败
Running 'npm audit fix' without understanding the dependency tree can introduce breaking changes or remove necessary packages, leading to runtime errors.
-
Deleting node_modules and reinstalling
50% 失败
Deleting node_modules and package-lock.json then reinstalling does not resolve the vulnerability if the vulnerable package version is pinned in package.json.
-
Using '--json' flag to suppress exit code
70% 失败
Using 'npm audit --json' to bypass the exit code might hide the vulnerability but doesn't address the policy; the CI will still fail if the policy is enforced at a higher level.