纱线和npm在实践中可以互换吗? [英] Are yarn and npm interchangeable in practice?

查看:88
本文介绍了纱线和npm在实践中可以互换吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含package.json文件和安装bash脚本的项目,该脚本在其他步骤中运行npm install.

I have a project with a package.json file and an install bash script that, among other steps, runs npm install.

我正在考虑更新脚本,以便在 yarn 可用的情况下运行yarn install(以利用该脚本)的高速缓存,锁文件等),否则返回到npm install.据我所知,所有软件包似乎都可以安装和正常工作.

I'm thinking of updating the script so that it runs yarn install if yarn is available (to take advantage of yarn's caching, lockfile, etc), and falls back to npm install otherwise. As far as I can tell, all the packages seem to install and work ok either way.

yarnnpm是否足够互换以便使之成为可行的方法?还是存在可能导致的潜在问题?我们是要选一根还是在实践中将纱线与npm互换?

Are yarn and npm interchangeable enough for this to be a viable approach, though? Or are there potential issues that this could lead to? Are we meant to just pick one, or is yarn interchangeable with npm in practice?

(nb.我已经阅读了密切相关的问题,但我在问这是一个单独的问题,因为它涉及在项目中显式支持yarn和npm安装过程)

(nb. I've read this closely related question, but I'm asking this as a separate question because it's about explicitly supporting both yarn and npm install processes in a project)

推荐答案

纱线和npm(版本> = 3.0.0)应该相对兼容,尤其是从npm 纱线,因为兼容性是纱线的既定目标之一.如从npm迁移所述:

Yarn and npm (version >=3.0.0) should be relatively compatible, especially moving from npm to Yarn, because compatibility is one of the stated goals of Yarn. As stated in Migrating from npm:

纱线可以使用与npm相同的package.json格式,并且可以从npm注册表中安装任何软件包.

Yarn can consume the same package.json format as npm, and can install any package from the npm registry.

因此,从理论上讲,任何对npm有效的package.json对于Yarn也应同样有效.请注意,我说npm v2可能不太兼容-这是因为npm从嵌套的node_modules结构迁移到平面布局(这是Yarn所使用的).就是说,Yarn和npm v3应该产生非常相似的布局,因为正如问题我链接:

So, in theory, any package.json that is valid for npm should also work equally well for Yarn. Note that I say that npm v2 is probably less compatible - this is because npm migrated from a nested node_modules structure to a flat layout (which is what Yarn uses). That said, Yarn and npm v3 should produce very similar layouts, because, as stated in the issue I linked:

首先,我们应该尝试与需要该兼容性的用户的node_modules布局高度兼容,因为这将是避免长尾兼容性问题的最可能方法.

To a first approximation we should try to be very compatible with the node_modules layout for people who need that compatibility, because it'll be the most likely way to avoid long-tail compatibility problems.

但是,您将无法利用Yarn生成的Yarn.lock,因为(顾名思义)它仅受Yarn支持,并且npm shrinkwrap不兼容.

However, you will not be able to take advantage of the Yarn.lock generated by Yarn, because (as the name suggests) it's only supported by Yarn, and npm shrinkwrap is not compatible.

此外,正如@RyanZim指出的那样,旧版本的Yarn不支持前后-install hooks ,但是版本晚于v0.16.1 .如果您依赖这些挂钩,则需要向用户指定要求版本大于v0.16.1的版本.

Also, as noted by @RyanZim, older versions of Yarn don't support pre- and post-install hooks, but versions later than v0.16.1 do. If you rely on these hooks, you will need to specify to users that versions greater than v0.16.1 are required.

总而言之,只要您没有遇到任何错误并且仅使用两个程序包管理器共享的功能,就不会有任何问题.

In summary, as long as you encounter no bugs and only use features that are shared by both package managers, you should have no issues whatsoever.

这篇关于纱线和npm在实践中可以互换吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆