npm audit fix

npm audit 命令

首先看官方文档,npm@6 的一大更新是新增了 npm audit 命令

Note: The npm audit command is available in npm@6. To upgrade, run npm install npm@latest -g.
The npm audit command submits a description of the dependencies configured in your package to your default registry and asks for a report of known vulnerabilities. npm audit checks direct dependencies, devDependencies, bundledDependencies, and optionalDependencies, but does not check peerDependencies.

该命令会在项目中更新或者下载新的依赖包之后会自动运行,如果你在项目中使用了具有已知安全问题的依赖,就收到官方的警告通知。
为了复现,随便找了一个没那么官方的项目 adminMongo 并执行 npm install 试试,果然

found 5 vulnerabilities (2 low, 1 moderate, 1 high, 1 critical)
  run `npm audit fix` to fix them, or `npm audit` for details

再执行下 npm audit 查看详情,列表很清晰。
在这里插入图片描述
执行 npm audit fix,发现并没有能自动的帮我 fix 掉这些错误。

fixed 0 of 5 vulnerabilities in 1295 scanned packages
  2 package updates for 5 vulns involved breaking changes
  (use `npm audit fix --force` to install breaking changes; or do it by hand)

根据提示尝试执行 npm audit fix --force,发现他帮我把包自动更新到了推荐版本(supertest@3.1.0,mocha@5.2.0)。
ps. 直接运行 --force 的行为不要学习,对于没能自动修复的问题,说明肯定出现了 SEMVER WARNING 之类的警告,这意味着推荐的修复版本存在让代码出问题的可能,主要发生在依赖包更改了 API 或者升级了大版本的情况下(semantic version major change,比如我这个项目的 supertest 版本是 1.2.0,而推荐的是 3.1.0)。这时候就需要格外的小心甚至需要改动一些自己的代码了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值