# npm ERR！代码 ELIFECYCLE
npm ERR！errno 1
npm ERR！audit：`npm audit`
npm ERR！退出状态 1
npm ERR！发现 1 个高严重性漏洞

- **ID:** `policy/npm-audit-fails-on-high-severity-vulnerability`
- **领域:** policy
- **类别:** build_error
- **错误码:** `ELIFECYCLE`
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

项目的 npm audit 策略（在 .npmrc 或 package.json 中配置）将高严重性漏洞视为构建失败，从而阻止 CI/CD 流水线。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| npm 8.x | active | — | — |
| npm 9.x | active | — | — |
| npm 10.x | active | — | — |

## 解决方案

1. ```
   使用 'npm audit fix --force'（如果可接受破坏性更改）更新易受攻击的包到修补版本，或手动在 package.json 中升级依赖。
示例：
  npm audit fix --force
  # 或手动：
  npm install semver@7.5.2 --save-exact
   ```
2. ```
   在 .npmrc 中覆盖审计策略，临时允许高严重性漏洞，将 'audit-level=high' 设置为 'audit-level=critical'。这仅适用于开发环境，不适用于生产。
示例：
  echo "audit-level=critical" >> .npmrc
  npm audit
   ```
3. ```
   使用第三方漏洞管理工具如 Snyk 或 Dependabot 自动修复漏洞并更新 PR，绕过 npm audit 的严格退出代码。
示例：
  # 安装 Snyk CLI
  npm install -g snyk
  snyk test --severity-threshold=high
   ```

## 无效尝试

- **Running 'npm audit fix' blindly** — Running 'npm audit fix' without understanding the dependency tree can introduce breaking changes or remove necessary packages, leading to runtime errors. (35% 失败率)
- **Deleting node_modules and reinstalling** — Deleting node_modules and package-lock.json then reinstalling does not resolve the vulnerability if the vulnerable package version is pinned in package.json. (50% 失败率)
- **Using '--json' flag to suppress exit code** — 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. (70% 失败率)
