Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于yarn #7

Open
39Er opened this issue May 9, 2017 · 0 comments
Open

关于yarn #7

39Er opened this issue May 9, 2017 · 0 comments

Comments

@39Er
Copy link
Owner

39Er commented May 9, 2017

首先来说一说yarn解决了npm的一些问题:

  • 安装的时候无法保证速度/一致性
  • 安全问题,因为 npm 安装时允许运行代码

yarn的另外一些优势:

  • yarn.lock锁定了依赖包的版本,保证其它机子也安装相同版本的包,同时包含了 package.json 中定义的一系列允许的版本。

  • yarn install 的时候会先使用缓存,所以再次下载速度会比npm快一些

  • yarn采用并行安装的方式

六大特性:

  • 离线模式: 一次安装,永久使用,无需下载
  • 依赖确定性:安装依赖锁定,保证一致性
  • 更好的网络性能:下载包,优化网络请求,最大限度提高网络利用率
  • 多注册来源处理:不管依赖包被不同的库间接关联引用多少次,安装这个包时,只会从一个注册来源去装(npm/bower), 防止出现混乱不一致。
  • 网络弹性处理: 安装依赖时,不会因为某个单次网络请求的失败导致整个安装挂掉。当请求失败时会进行自动重试。
  • 扁平模式: 当关联依赖中包括对某个软件包的重复引用,在实际安装时将尽量避免重复的创建

yarn & npm 常用命令比较

npm install === yarn / yarn install

npm install xxx —save === yarn add xxx

npm uninstall xxx —save === yarn remove xxx

npm install xxx —save-dev === yarn add xxx —dev

npm update === yarn upgrade

npm install xxx -g === yarn global add xxx

但个人用过一段时间yarn之后有了这样的反思:

我真的需要yarn吗?

  在使用yarn时发现,但项目所依赖的包a如果依赖其他包b(semver '~')时,yarn会锁定这个b的版本,而当b包有小版本更迭时,直接使用yarn upgrade a 依旧不会更新 b的版本,这点就像npm update a 一样。这时如果删除node_modules,重新yarn intall 时,就会出现问题,因为yarn.lock已经锁定了b的版本,所以再次install依旧安装的是老版本的,也就是说这样下去可能只有等到a包的下一个版本更迭才能更新到b包的新版本,所以这时需要将yarn.lock也一并删除,或 yarn remove a,重新 install a,但这样与npm 无异,且一不小心可能会将b的老版本一直锁在yarn.lock中。在使用antd的时候就发现了这个问题,其中依赖rc-select包,当rc-select更新时,只有re-install antd 才能更新rc-select。

另外看了死马的这篇文章后加深了这种反思。也许在今后更多的使用中我会有新的发现和感想。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant