node yarn

by Alcides Queiroz

通过Alcides Queiroz

如何使用Yarn Plug'n'Play摆脱node_modules (How to get rid of node_modules with Yarn Plug’n’Play)

将您的安装时间减少多达70%。 问我如何! ? (Reduce your install time up to 70%. Ask me how! ?)

Anyone who knows me can confirm that I’m a long-standing lover of JavaScript and its entire ecosystem. As a Front-end engineer, Node-based package managers have been a crucial part of my toolset since 2013.

任何认识我的人都可以确认我是JavaScript及其整个生态系统的长期爱好者。 自2013年以来,作为一名前端工程师,基于节点的软件包管理器一直是我工具集的关键部分。

First, I used Bower, which was primarily focused on the front-end world. Then, in 2015, I sadly (ok, not really) realized that Bower was dying and NPM, the default package manager for Node, was the way to go for the front-end too. It was strange for me, at first, to use NPM for other things than Node modules, but I got used to the idea and migrated seamlessly.

首先,我使用了Bower,它主要专注于前端世界。 然后,在2015年,我可悲的是(很好, 不是真的 )意识到Bower快要死了,而Node的默认软件包管理器NPM也是去前端的一种方式。 起初,我将NPM用于Node模块之外的其他事情对我来说很奇怪,但是我习惯了这个想法并无缝迁移。

Finally, just one year later, Facebook gave us Yarn, a modern and blazing fast alternative to NPM. I loved it at first sight! But some things were still problematic…

终于,仅仅一年后,Facebook给了我们Yarn,这是NPM的现代而又快速的替代方案。 我一见钟情! 但是有些事情还是有问题的……

纱线中的继承问题 (Inherited problems in Yarn)

Besides the speed, Yarn brought a number of advantages when compared to the NPM version at the time, such as lock files, offline mode, network resilience, checksums and others. Nevertheless, Yarn borrowed some known problems from NPM:

除了速度之外,与当时的NPM版本相比,Yarn还带来了许多优势,例如锁定文件,脱机模式,网络弹性,校验和等。 但是,Yarn从NPM借来了一些已知的问题:

这里到处都有node_modules (node_modules here, there, everywhere)

For each project on your machine that uses NPM or Yarn, a node_modules folder is created. It doesn't matter if 10 projects use the exact same version of a given module, it will be copied over and over into each node_modules folder of these projects.

对于计算机上使用NPM或Yarn的每个项目, node_modules创建一个node_modules文件夹。 十个项目是否使用给定模块的完全相同的版本都没关系,它将一遍node_modules复制到这些项目的每个node_modules文件夹中。

生成新的node_modules文件夹需要很长时间 (Generating a new node_modules folder takes a really long time)

Even taking a great leap forward in terms of installation speed, Yarn was constrained by node_modules limitations. Just creating the node_modules folder takes up to 70% of the time needed to run yarn install (with a warm cache). It's a huge amount of files to be created on every installation. So, don't blame it on Yarn.

即使在安装速度方面取得了长足的进步,Yarn仍然受到node_modules限制的约束。 仅创建node_modules文件夹最多会花费70%的时间运行yarn install (带有热缓存)。 每次安装都会创建大量文件。 因此,不要将其归咎于Yarn。

依赖项未添加到package.json (Dependencies not added to package.json)

Here’s a scenario for you: Your app works perfectly in development, but crashes in production. After hours of investigation, you finally realize that you forgot to add a dependency to your package.json. Yes, it can happen.

这是为您提供的一种方案:您的应用程序在开发中运行完美,但在生产中崩溃。 经过数小时的调查,您终于意识到,您忘记为package.json添加依赖项。 是的,有可能发生。

运行时模块分辨率慢 (Slow module resolution at runtime)

The boot time of your app is heavily impacted by the way Node resolves dependencies. It wastes time querying the file system to discover where a given dependency will be resolved from.

您的应用程序的启动时间受节点解决依赖关系的方式的严重影响。 浪费时间查询文件系统以发现将从中解析给定依赖项的时间。

毛线即插即用! (Yarn Plug’n’Play to the rescue!)

All of the above problems were addressed by the Yarn team with the release of the Plug’n’Play feature last September.

去年9月,Yarn团队发布了Plug'n'Play功能,解决了上述所有问题。

When you enable PnP, instead of copying every needed file from the cache to the node_modules folder, here's what Yarn does:

启用PnP后,Yarn会执行以下操作,而不是将每个需要的文件从缓存复制到node_modules文件夹中:

  1. It creates a single file with static resolution tables. These tables will contain a bunch of important info, such as: packages available in the dependency tree, how they relate between themselves and their location on the disk.它使用静态分辨率表创建一个文件。 这些表将包含一堆重要信息,例如:依赖关系树中可用的软件包,它们之间的关系以及它们在磁盘上的位置。
  2. A special resolver is used in order to help Node discovering where each dependency has been installed (under the Yarn cache folder). It solely relies on the resolution tables which were created previously. As these tables contain information about the entire dependency tree, the node_modules resolution process won’t need to make a lot of stat and readdir calls at runtime anymore, significantly reducing your app boot time. And as Yarn knows all of your dependencies, it will complain if you try to import a module that's not present in your package.json:

    为了帮助Node发现每个依赖项的安装位置(在Yarn缓存文件夹下),使用了一个特殊的解析器。 它仅依赖于先前创建的分辨率表。 由于这些表包含有关整个依赖关系树的信息,因此node_modules解析过程不再需要在运行时进行大量的statreaddir调用,从而大大减少了应用程序的启动时间。 而且由于Yarn知道您的所有依赖关系,因此如果您尝试导入package.json不存在的模块,它将发出抱怨:

使用毛线即插即用 (Using Yarn Plug’n’Play)

Converting a project to make use of PnP is easy as 1–2–3. You just need to add a installConfig section to your package.json, with a pnp key set to true, like this one:

转换项目以使用PnP的过程很简单,为1-2-3。 您只需要在package.json添加installConfig部分,并将pnp密钥设置为true ,就像这样:

{    "installConfig": {     "pnp": true   }}

Note: You need Yarn v1.12+ in order to use Plug’n’Play.

注意:您需要使用Yarn v1.12 +才能使用Plug'n'Play。

After that, just run yarn install and everything inside your node_modules folder will be deleted. From now on, every dependency will be resolved directly from Yarn's hot cache.

之后,只需运行yarn installnode_modules文件夹内的所有内容都将被删除。 从现在开始,每个依赖关系都将直接从Yarn的热缓存中解决。

在带有create-react-app的新React项目中使用PnP (Using PnP in a new React project with create-react-app)

If you use create-react-app 2+, the good news is that it works great with Yarn Plug’n’Play! Just append the --use-pnp option to the create-react-app command and you're good to go:

如果您使用create-react-app 2+,那么好消息是它可以与Yarn Plug'n'Play一起使用! 只需将--use-pnp选项附加到create-react-app命令,就可以了:

npx create-react-app your-app-name --use-pnp

可能的问题 (Possible issues)

As nothing is perfect in the world, PnP may incur new issues when used in projects relying on a custom install logic. If you need more info about these potential new issues, you can find a detailed explanation in this paper.

由于世界上没有什么是完美的,PnP在依赖自定义安装逻辑的项目中使用时可能会引发新的问题。 如果您需要有关这些潜在新问题的更多信息, 可以在本文中找到详细的说明 。

结论 (Conclusion)

Plug’n’Play solves some really annoying problems in Yarn. Besides, it dramatically improves dependency caching on CIs, saving installation time and allowing our builds to get right to the point: running the tests!

Plug'n'Play解决了Yarn中一些非常烦人的问题。 此外,它极大地改善了对CI的依赖项缓存,节省了安装时间,并使我们的构建工作得以进行: 运行测试!

And that’s it! Have fun with Yarn PnP.

就是这样! 玩Yarn PnP。

翻译自: https://www.freecodecamp.org/news/getting-rid-of-node-modules-with-yarn-plugn-play-a490e5e747d7/

node yarn

node yarn_使用Yarn Plug'n'Play摆脱node_modules相关推荐

  1. Linux Centos7 以及window 10 Kali2020.4 安装 nvm node npm cnpm yarn

    nvm node - npm - cmpm yarn 过程 安装nvm 管理工具 ps | 安装时候 安装不上把http 改成git 1 打开终端 执行以下命令 wget -qO- https://r ...

  2. hadoop的yarn与node中的yarn冲突问题

    因为需要编译superset的关系. 编译过程中需要用到node的yarn 先把~/.bashrc中的下面这句话注释掉: export PATH=$PATH:$HADOOP_HOME:$HADOOP_ ...

  3. mac下npm/node的安装和卸载、升级;node、npm升级后最后删掉node_modules重新安装

    mac还是使用brew install简单一些:最好使用一种安装方式,不要多种方式互用: 更新npm到最新版本npm install -g npm更新npm到指定版本 npm -g install n ...

  4. 解决win7被node.js抛弃的问题 - 1)npm/node_modules/node/bin/node: line 1: This: command not found 2)win8.1或以上

    效果图 出现过的问题,以及解决办法 问题1: C:\Users\mac\AppData\Roaming\npm/node_modules/node/bin/node: line 1: This: co ...

  5. YARN node labels

    最近在做实验,实验需要进行分区域计算,网上查了资料后发现Yarn Node Labels + Capacity-Scheduler可以实现我的需求 但是当任务提交到capacity-scheduler ...

  6. 【总结】1409- 深入浅出 npm yarn pnpm 包管理机制

    前端开发者们每天都在接触 xxx install,包管理器是必不可少的工具. 本文会以尽量简洁的语言来描述当下主流包管理工具 npm.yarn.pnpm 的管理策略以及进化史,不涉及任何晦涩的代码. ...

  7. 深入浅出 npm yarn pnpm 包管理机制

    前端开发者们每天都在接触 xxx install,包管理器是必不可少的工具. 本文会以尽量简洁的语言来描述当下主流包管理工具 npm.yarn.pnpm 的管理策略以及进化史,不涉及任何晦涩的代码. ...

  8. 认识Web前端、Web后端、桌面app和移动app新开发模式 - 基于Node.js环境和VS Code工具...

    认识Web.桌面和移动app新开发模式 - 基于Node.js环境和VS Code工具 一.开发环境的搭建(基于win10) 1.安装node.js和npm 到node.js官网下载安装包(包含npm ...

  9. Vue.js-Day03-AM【超级详细:Node.js环境安装、安装淘宝镜像(Win、Mac)、安装Vue脚手架、初始化Vue项目-命令解释(Vscode、命令行窗口)、目录介绍、Vue文件介绍】

    Vue.js实训[基础理论(5天)+项目实战(5天)]博客汇总表[详细笔记] 目   录 1.Node.js-环境安装 1.1.Node.js-详细安装步骤 2.vue-cli脚手架安装 2.1.安装 ...

最新文章

  1. 【Vue学习第三天】组件的使用
  2. 剑指Offer题解(Python版)
  3. vb net的定时循环_.NET工具ReSharper:如何帮助Visual Studio用户?
  4. Pytorch 之autograd
  5. ionic 配置java_Ionic 项目配置环境
  6. 这个星期压力貌似比较大啊!
  7. redis主从、哨兵、集群的区别
  8. (转)马云又在押宝智能投顾!
  9. SQL Server数据库培训实战视频教程(2天速成版)
  10. 软件工程---团队作业4
  11. Blast 几种方法的具体用法以及含义
  12. oracle log network server,记录一次ARC1: Destination LOG_ARCHIVE_DEST_2 network reconnect abandoned解决...
  13. Linux 安装molten
  14. poi修改excel单元格值导致单元格变成橙色的问题(WPS)
  15. win7系统 邮箱服务器搭建,win7搭建邮箱服务器
  16. 最近在读的一些文章-2019.04
  17. 联想3650服务器配置信息,【联想x3650参数】联想x3650系列服务器参数-ZOL中关村在线...
  18. 牛客华为题库前40题
  19. HP Color LaserJet MFP M181fw 耗材问题解决办法
  20. 【进阶4-2期】Object.assign 原理及其实现

热门文章

  1. HTML5 css链接添加不同的样式
  2. 多线程爬虫工作流程图 to 子年
  3. 能够使用StringBuilder类的常用方法操纵字符串 1215
  4. 用代码建立与数据库的连接 c#连sqlserver
  5. Navicat Premium 12安装过程和相关资源
  6. 190829课堂母版与子版
  7. Elasticsearch 定制评分(自定义评分)
  8. MacVim配置目录树
  9. Ubuntu更换apt镜像源
  10. SpringCloud(九):sidecar异构微服务