微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – Yarn vs Npm – “在我的机器上工作” – 澄清?

我是纱线新手,在阅读 this article时有些东西引起了我的注意:

Deterministic:
The same dependencies will be installed the same exact
way across every machine regardless of install order. Yarn resolves
“works on my machine” issues around versioning and non-determinism by
using lockfiles and an install algorithm that is deterministic and
reliable

题:

我不明白:当我编写npm install时,它会查看package.json并安装确切的版本,每个版本也根据自己的package.json安装其依赖项,依此类推

那么有什么不同(关于这个方面)

将会非常感谢“在没有纱线的情况下npm可能出错的情况”的情景示例

解决方法

package.json文件通常包含依赖项所需的最低版本.例如,您可以使用“^ 1.0.0”,它与版本1.0.0或任何次要版本匹配.

{
“name”:“my_package”,
“版本”:“1.0.0”,
“依赖”:{
“my_dep”:“^ 1.0.0”
}
}

当你运行npm install时,它可以安装“my_dep”的版本1.0.0,1.1.0,1.2.0等,因为所有这些版本都满足package.json的要求.您最终可以在本地计算机上使用1.0.0版本,在测试环境中使用1.1.0.

纱线会自动创建一个yarn.lock文件,以确保始终安装相同版本的“my_dep”.它生成这样的东西:

# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
my_dep@^1.0.0:    
version "1.1.0"
resolved "https://registry.npmjs.org/my_dep/-/my_dep-1.1.0.tgz#a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0"

Yarn将使用此文件将“my_dep”解析为1.1.0版,即使有新版本(1.2.0)可用.

All yarn.lock files should be checked into source control (e.g. git or mercurial). This allows Yarn to install the same exact dependency tree across all machines,whether it be your coworker’s laptop or a CI server.

参考文献:

https://docs.npmjs.com/getting-started/using-a-package.json

https://docs.npmjs.com/getting-started/semantic-versioning

https://yarnpkg.com/en/docs/yarn-lock

原文地址:https://www.jb51.cc/js/156792.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐