单片存储库和多个 node_modules 文件夹 [英] Monolithic repo and multiple node_modules folder

查看:63
本文介绍了单片存储库和多个 node_modules 文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用单体存储库的项目.每个包都有自己的 package.json 文件来管理所述包的依赖关系.我正在使用 lerna 在我们的 monorepo 应用程序的所有子包"中安装包.

I have a project using monolithic repositories. Each packages has his own package.json file to manage dependencies for the said package. I'm using lerna to install package in all "sub-packages" of our monorepo app.

当前项目结构如下

Project/ 
| package.json
| node_modules/
|- packages/
|-- package1/ 
|--- package.json 
|--- node_modules/
|-- package2/
|--- package.json
|--- node_modules/

我正在研究一种在根 node_modules 文件夹中概括通用依赖项的方法,以便每个包在运行 lerna exec -- npm install 时不会提取自己的节点包副本,而是取而代之使用位于单体存储库根部的那个,这样我们就可以避免在多个存储库中安装相同的包,从而减少项目的大小.

I'm looking into a way of generalize common dependencies in the root node_modules folder so each packages doesn't pull his own copy of a node package when running lerna exec -- npm install but instead use the one that is at the root of the monolithic repo so we avoid installing the same package in multiple repo, hence, reducing the size of the project.

我已经看到了一些解决方案,包括在项目之间建立一些符号链接,但这似乎不是一门精确的科学,因为符号链接支持是非常操作系统自以为是的.此外,这似乎不是一种受支持的方式.

I've seen some solution including to make some symlinks between project but that's doesn't seems to be an exact science since symlink support is very OS opinionated. Also, this doesn't seems to be a supported way of doing it.

目前,我们才刚刚开始,在运行 lerna exec -- npm install 之后,该项目已经在磁盘上大约 350 mb 并且第一次从 npm 中提取所有内容大约需要 5 分钟.由于项目会随着时间的推移而增长,这个时间也会随着时间的推移而延长...

Currently, we're just at the beginning and after running lerna exec -- npm install the project is already around 350mb on disk and pulling everything from npm takes around 5 minutes the first time. As the project will grow over time, this time will also extend over time...

所以为了恢复一切,我正在寻找一种方法来提取 repo 根目录下 node_modules 文件夹中的公共依赖项,并使子包从此文件夹中提取其公共依赖项,而不是每次都获取自己的副本.

So to resume everything, I'm looking for a way to extract the common dependencies in a node_modules folder at the root of the repo and make the sub-packages pull from this folder their common dependencies instead of getting their own copies everytime.

推荐答案

Lerna 最近添加了一个 --hoist 选项,看起来它正好提供了您在此处寻找的内容.它将外部依赖项安装在 repo 根目录下,以便所有包都可以使用它们.二进制文件链接到依赖包 node_modules/.bin/ 目录中,因此它们可用于 npm 脚本.

Lerna recently added a --hoist option that looks like it provides exactly what you were looking for here. It installs external dependencies at the repo root so they're available to all packages. Binaries are linked into dependent package node_modules/.bin/ directories so they're available for npm scripts.

它可以通过命令行传递或添加到 lerna.json 以进行持久配置.

It can be passed on the command-line or added to lerna.json for durable configuration.

文档可在此处获得:https://github.com/lerna/lerna#--hoist-glob

这篇关于单片存储库和多个 node_modules 文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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