npm更新项目包
大约 1 分钟
提示
因为npm包都是放在github上,网络原因下载缓慢,所以命令后面都加上 --registry=https://registry.npm.taobao.org
更新单个 npm 包
1更新到最新版本(包括大版本更新)
npm install <package-name>@latest --registry=https://registry.npm.taobao.org
例如:
npm install lodash@latest --registry=https://registry.npm.taobao.org
2更新到最新的次要版本或补丁版本
npm install lodash@latest --registry=https://registry.npm.taobao.org
更新所有包
1使用 npm update
npm update --registry=https://registry.npm.taobao.org
2使用 npm-check-updates
提示
npm-check-updates 是一个工具,可以检查 package.json 文件,并更新所有依赖到最新的版本(包括大版本更新)。
首先,全局安装 npm-check-updates:
npm install -g npm-check-updates --registry=https://registry.npm.taobao.org
然后,运行以下命令来检查更新并更新 package.json 文件:
ncu -u
这将更新 package.json 文件中的所有依赖版本。
最后,运行 npm install 来安装更新后的依赖:
npm install --registry=https://registry.npm.taobao.org
更新全局安装的 npm 包
npm update -g <package-name> --registry=https://registry.npm.taobao.org
检查更新状态
在更新包之前,你可能希望先检查一下当前安装的包有哪些更新版本。可以使用 npm outdated 命令来查看:
npm outdated
这个命令会列出当前版本、想要的版本和最新版本。
更新特定版本
如果你需要更新到某个特定版本,可以指定版本号:
npm install <package-name>@<version>
例如:
npm install lodash@4.17.20 --registry=https://registry.npm.taobao.org