yarn upgrade为什么不更新package.json

December 10, 2019 ... ☕️ 3 min read

项目跑一段时间,或者想支持新特性,或者单纯怕被人说太out,就想把依赖包升级到较新版。Yarn upgrade即用于升级依赖。

比如我想升级css-loader,目前用的是3.2.1

{
  "css-loader": "^3.2.1",
}

yarn upgrade: Upgrades packages to their latest version based on the specified range.

直接运行yarn upgrade css-loader,输出

css-loader-upgrade-console

看起来很完美,升级到了3.3.0。但是,打开package.json,还是"css-loader": "^3.2.1",emmmm…😕

yarn list看看

yarn list

使用的是最新版了。lockfile里的内容

lockfile

看起来是使用了3.3.0,但是标签还是3.2.1?如何把两个文件搞同步了呢?

yarn upgrade为什么不更新package.json?

The package.json file will be updated to reflect the latest version range. By default, the existing range specifier in package.json will be reused if it is one of: ^, ~, <=, >, or an exact version. Otherwise, it will be changed to a caret (^). One of the flags —caret, —tilde or —exact can be used to explicitly specify a range.

翻译一下:如果package.json对应的包名版本,包含了^undefined ~undefined <=undefined >符号,就会重用,否则更新。WTF…

如果用yarn upgrade css-loader --latest

可能会更新package.json,情况是:大版本更新。

The upgrade —latest command upgrades packages the same as the upgrade command, but ignores the version range specified in package.json. Instead, the version specified by the latest tag will be used (potentially upgrading the packages across major versions)

啥是major versions?比如版本是1.2.3,1就是major version,2是minor version,3是revision(或subminor version)。

yarn upgrade-interactive

这个命令和upgrade基本格式类似,会在upgrade之前列出过时的包,让用户选择更新到哪个版本。

可以把这个命令看做yarn outdatedyarn upgrade的组合命令。

--latest,会忽略package.json指定的版本,直接使用标记latest的版本。

最后,如何更新lockfile,又更新package.json

执行yarn upgrade-interactive css-loader --latest,然后根据交互,选择一个。

或者干脆yarn add css-loader吧。

#yarn