使用npm更新所有依赖包到最新版本

本篇文章参考文章 - npm包之npm-check-updates

npm-check-updates

  一键升级所有依赖的插件为 npm-check-updates需要执行以下步骤:

安装
npm  install -g npm-check-updates
检查
npm-check-updates      //  检查当前项目中有没有哪些依赖包可更新(简写ncu)

  检查结果如下所示:
在这里插入图片描述

更新
ncu -u // 更新package.json

  注意上面命令是更新 package.json ,真正的依赖包还没有下载下来,所以需要删除 node_modules 重新 install

查看命令
ncu --help   // 查看相关命令

背景交代

  找到上面那个插件是因为最近接到个新项目,应领导要求,为了紧追时代脚步,加强公司技术沉淀,改革现有应对方案,准备升级当前开发框架,向国际一线大厂看齐。一切采用最新,最快,最高标准。

  抓紧实现弯道超车,大踏步进军,争做区域霸主,全球龙头,最终实现世界和平!(鼓掌)

  上面是我意淫的,主要是最近的项目需要把所有依赖库升级到最新版本。

  一开始不知道有一键升级的快捷方式,苦逼的我一条一条手动升级,简直不要太酸爽,最头疼的是,有些依赖库版本太低,升级过程中出现了不兼容情况,只能升级到当前大版本的最新补丁版本。比如 react ,现有项目是 16.13.1 ,执行 npm install react 之后只能更新到 16.14.0 ,如果执行 npm install [email protected] ,那么会提示下面这个报错:

Found: [email protected]
npm ERR! node_modules/react
npm ERR!   react@"17.0.1" from the root project
npm ERR!   peer react@"*" from @icons/[email protected]
npm ERR!   node_modules/@icons/material
npm ERR!     @icons/material@"^0.2.4" from [email protected]
npm ERR!     node_modules/react-color
npm ERR!       react-color@"^2.17.3" from the root project
npm ERR!   17 more (antd, echarts-for-react, element-react, rc-drawer, ...)
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer react@"^0.14.0 || ^15.0.0 || ^16.0.0" from @ant-design/[email protected]
npm ERR! node_modules/@ant-design/create-react-context
npm ERR!   @ant-design/create-react-context@"^0.2.4" from [email protected]
npm ERR!   node_modules/antd
npm ERR!     antd@"^3.20.3" from the root project
npm ERR!   @ant-design/create-react-context@"^0.2.4" from [email protected]
npm ERR!   node_modules/rc-mentions
npm ERR!     rc-mentions@"~0.4.0" from [email protected]
npm ERR!     node_modules/antd
npm ERR!       antd@"^3.20.3" from the root project
npm ERR!   2 more (rc-tabs, rc-tree)
npm ERR!
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
npm ERR!

  上面问题出现的原因是因为 node 版本太高,不兼容当前升级的依赖,解决方法是把 node 降级到稳定版本就可以了。

  我估计开发 npm-check-updates 插件的老哥,也是嫌麻烦,就做了个一键升级,解决所有烦恼。

猜你喜欢

转载自blog.csdn.net/EcbJS/article/details/112304193