最近我试着用Yarn 安装我的 node 包.它工作得很好,比NPM快得多.Yarn 自动生成yarn.lock
条Yarn .我们已经有了NPM收缩膜(npm-shrinkwrap.json
).
他们之间有什么区别吗?
最近我试着用Yarn 安装我的 node 包.它工作得很好,比NPM快得多.Yarn 自动生成yarn.lock
条Yarn .我们已经有了NPM收缩膜(npm-shrinkwrap.json
).
他们之间有什么区别吗?
yarn.lock
文件与其他包管理器的锁文件非常相似,尤其是Rust的Cargo package manager,它有Cargo.lock
个锁.这些锁文件的 idea 是代表一组应该始终工作的一致的包.
npm
在package.json
文件中存储依赖范围,这意味着当有人安装您的软件包时,他们可能会向您获取不同的依赖集,因为您可能正在运行过时的软件包(尽管它们仍然满足您指定的依赖范围).例如,有人指定了依赖项"foo": "^1.0.0"
.他们可能已经安装了foo v1.0.1,因为这是他们运行npm install
时的最新版本,但后来,有人安装了您的包,并获得了依赖项foo v1.1.0. 这可能会意外地 destruct 某些东西,如果您有一个保证consistent package resolution的yarn.lock
文件,这是可以避免的.
与npm shrinkwrap
相比,the documentation解释得非常清楚:
它类似于npm的npm收缩膜.然而,json并没有损耗,它可以创建可复制的结果.
该文档还建议,如果您还没有这样做,请将yarn.lock
提交到您的存储库中,这样您就可以获得一致且可复制的包解析的好处.This question还进一步解释了为什么你应该这样做.
The lossy behaviour of npm shrinkwrap
is due to the non-deterministic algorithms used by npm
itself; as stated in the comments of another answer, npm shrinkwrap
> npm install
> npm shrinkwrap
is not guaranteed to produce the same output as just shrinkwrapping once, whereas Yarn explicitly uses "an install algorithm that is deterministic and reliable".