Skip to main content

旧包管理

¥Legacy Package Management

¥Migrating from lerna bootstrap, lerna add and lerna link in lerna v7 and later

在 lerna v7.0.0 中,我们默认删除了 lerna 中的 lerna bootstraplerna addlerna link 命令。

¥In lerna v7.0.0, we removed the lerna bootstrap, lerna add and lerna link commands in lerna by default.

本节介绍如何最好地放弃使用它们并使用包管理器 workspaces 使你的设置现代化。有关为什么需要这样做的完整背景,请参阅下面的 背景

¥This section covers how best to migrate away from using them and modernize your setup using package manager workspaces. For full context on why this necessary, see Background below.

重要的思维转变是认识到 lerna 不负责安装和链接存储库中的依赖,你的包管理器更适合该任务。

¥The important mental shift is to recognize that lerna is not responsible for installing and linking your dependencies in your repo, your package manager is much better suited to that task.

实现此目的的方法是使用包管理器的 workspaces 功能。请参阅此处各自的文档:

¥The way to achieve this is by using your package manager's workspaces feature. See their respective documentation here:

通过使用 workspaces,你的包管理器将执行与 lerna bootstraplerna link 之前为你执行的完全相同的链接,只不过它直接烘焙到 install 命令中。运行安装后不需要其他命令(只要你按照上面的包管理器文档配置了 workspaces)。

¥By using workspaces, your package manager will perform the same exact linking that lerna bootstrap and lerna link were doing for you before, except it is baked right into the install command. No additional commands after running an install are necessary (as long as you have workspaces configured per the package manager documentation above).

更换 lerna add 时也是如此。添加和删除依赖是你的包管理器已经为你做的事情,并且由于 workspaces 是一流的用例,你可以运行适当的 install 命令来将依赖添加到特定的包/工作区,以及所有相关的本地链接 将自动发生。

¥The same thing goes for replacing lerna add. Adding and removing dependencies is something your package manager already does for you, and because workspaces is a first class use case, you can run an appropriate install command to add a dependency to a specific package/workspace and, again, all the relevant local linking will take place automatically.

请参阅下文了解更具体的比较以及使用前后的比较。

¥See below for more concrete comparisons, and before and after usage.

¥Replacing your usage of lerna bootstrap/lerna link

它有什么作用?

¥What does it do?

使用 lerna bootstrap 代替 npm install(或 yarn/pnpm)。它将安装所有外部包并链接工作区中的所有内部包。lerna link 将仅执行此操作的内部链接步骤。

¥lerna bootstrap was used in place of npm install (or yarn/pnpm). It would install all external packages and link all internal packages within the workspace. lerna link would just perform the internal linking step of this operation.

我在哪里可以找到它?

¥Where would I find it?

它很可能位于工作区根目录中 package.json 的 "scripts" 属性中。另请检查你的 CI 管道,因为它们也可能会调用 lerna bootstrap 来代替 npm install(或 yarn/pnpm)。

¥It would most likely be in the "scripts" property of package.json in the root of your workspace. Also check your CI pipelines, as they might also be calling lerna bootstrap in place of npm install (or yarn/pnpm).

我用什么来代替它?

¥What do I replace it with?

lerna bootstrap 替换为 npm install(或 yarn/pnpm)。如果你已经在之前调用 lerna bootstrap 的工作流程中的某处执行了包管理器的安装命令,那么你可以将其删除。lerna link 可以删除,因为链接步骤现在由包管理器在 npm install 期间处理。

¥Replace lerna bootstrap with npm install (or yarn/pnpm). If you are already performing your package manager's install command somewhere in your workflow before where you had previously called lerna bootstrap, then you can just delete it instead. lerna link can just be removed, as the linking step is now handled by your package manager during npm install.

信息

如果你使用 yarn 并依赖于链接二进制文件,那么你可能需要在切换到工作区后删除 node_modules 文件夹一次。详情参见 这个 Yarn 问题

¥If you are using yarn and depend on linking binaries, then you may need to delete your node_modules folder once after switching to workspaces. For details, see this yarn issue.

替换你对 lerna add 的使用

¥Replacing your usage of lerna add

它有什么作用?

¥What does it do?

lerna add 用于添加对工作区中的包的依赖。它将更新每个包的 package.json 文件以添加依赖。

¥lerna add was used to add a dependency to packages in the workspace. It would update the package.json files of each package to add the dependency.

我在哪里可以找到它?

¥Where would I find it?

虽然通常是手动调用的,但 lerna add 可能会在工作区根目录中的 package.json 中的某些脚本中找到。

¥Though usually called manually, lerna add might be found in some scripts in package.json in the root of your workspace.

我用什么来代替它?

¥What do I replace it with?

lerna add 大部分可以替换为 npm install(或 yarn/pnpm)的变体。lerna add 最常见的用例是向工作区中的单个包添加单个依赖。该命令如下所示:

¥lerna add can mostly be replaced with a variation of npm install (or yarn/pnpm). The most common use case for lerna add was to add a single dependency to a single package within the workspace. This command looks like:

lerna add <dependency> --scope <package>

并且可以直接替换为:

¥and can be replaced directly with:

npm install <dependency> -w <package>

-w 标志告诉 npm 仅在 <package> 指定的工作区包中安装依赖,类似于 Lerna 的 --scope 选项。

¥The -w flag tells npm to only install the dependency in the workspace package specified by <package>, similar to the --scope option for Lerna.

如果需要为多个包添加依赖,可以多次使用 -w 选项:

¥If you need to add a dependency to multiple packages, you can use the -w option multiple times:

npm install <dependency> -w <package1> -w <package2>

定制提升

¥Custom Hoisting

lerna 的旧版 bootstrap 命令的好处之一是它为你提供了围绕提升或不提升某些依赖到存储库的根目录或将它们保留在嵌套位置的控制。

¥One of the nice things about lerna's legacy bootstrap command was the control it offered you around hoisting or not hoisting certain dependencies up to the root of the repo, or leaving them in nested locations.

因此,如果你在包提升方面有相当自定义的设置,你可能会担心从 lerna bootstrap 迁移。

¥Therefore you may be concerned about migrating away from lerna bootstrap if you have quite a custom setup in terms of package hoisting.

根据我们测试各种包管理器的经验,我们发现现代 yarn(即 v3 及更高版本)在提升控制方面提供了最大的灵活性:

¥In our experience of testing out the various package managers, we have found that modern yarn (i.e. v3 and later) offers the most flexibility in terms of hoisting controls:

https://yarn.nodejs.cn/configuration/yarnrc/#nmHoistingLimits

我们还没有找到一个 lerna bootstrap 动力仓库,无论提升复杂程度如何,都无法转换为现代 Yarn,所以如果这适用于你,请尝试一下。

¥We have yet to find a lerna bootstrap powered repo, no matter the hoisting complexities, that could not be converted to modern yarn, so please try it out if this applies to you.

如果你只是使用 lerna bootstrap 而没有任何高级提升问题,请随意选择任何包管理器,它们都提供强大的 workspaces 实现。

¥If you were just using lerna bootstrap without any advanced hoisting concerns, feel free to choose from any of the package managers, they all offer robust workspaces implementations.

暂时填充旧包管理命令

¥Temporarily polyfilling legacy package management commands

如果你真的发现自己陷入困境并且需要 v7 中的 lerna bootstraplerna addlerna link 的旧包管理命令,你可以安装与 lerna 包相同版本的 @lerna/legacy-package-management 包,这将使用旧的实现来填充命令。

¥If you really find yourself stuck and needing the legacy package management commands of lerna bootstrap, lerna add and lerna link in v7, you can install the @lerna/legacy-package-management package at the same version as your lerna package, and this will polyfill the commands with their old implementations.

需要注意的是,这只是一个权宜之计,这个新软件包只能被认为处于维护模式 - 对于旧版包管理问题(例如 lerna bootstraplerna addlerna link),不会考虑任何新功能,并且我们只会合并关键补丁和安全更新。

¥It's important to note that this is just a stop gap and this new package can be thought of as being in maintenance mode only - no new features will be considered for legacy package management concerns (such as lerna bootstrap, lerna add and lerna link), and we will only look to merge critical patches and security updates.

如果你发现自己处于这种情况,请在 lerna 存储库上打开一个问题,以便我们可以更多地了解你所面临的困难并帮助你找到前进的方向:

¥If you find yourself in this position, please open an issue on the lerna repo so that we can learn more about the difficulties you are facing and help you find a way forward:

https://github.com/lerna/lerna/issues/new/choose

背景

¥Background

Lerna 是 JavaScript 生态系统中最初的 monorepo/workspace 工具。当它于 2015/2016 年创建时,生态系统看起来完全不同,并且没有内置功能来处理单个存储库("workspace")中的多个包。像 lerna bootstraplerna addlerna link 这样的命令都是 lerna 项目的关键部分,因为没有其他选择。

¥Lerna is the original monorepo/workspace tool in the JavaScript ecosystem. When it was created in 2015/2016 the ecosystem looked totally different, and there were no built in capabilities to handle working with multiple packages in a single repository (a "workspace"). Commands like lerna bootstrap, lerna add and lerna link were all a critical part of the lerna project, because there were no other options.

然而,事实是 - 多年来 - 我们所了解和喜爱的包管理器(npmyarnpnpm)都完全支持将工作区作为一流用例的概念。

¥However, the fact is that - for many years now - the package managers we know and love (npm, yarn and pnpm) all fully support that concept of workspaces as a first-class use-case.

他们已经过经过实战测试的实现,包括添加、删除和链接本地包,以及以自然的方式将它们与第三方依赖结合起来。

¥They have battle tested implementations covering adding, removing and linking local packages, and combining them with third party dependencies in a natural way.

这就是为什么 Daniel 在担任 lerna 首席维护者的最后几年里,一直鼓励人们强烈重新考虑在 lerna 中使用旧版包管理命令,而是利用他们选择的包管理器来执行以下操作: 它最擅长什么。

¥This is the reason why, for the final several years of his tenure as lead maintainer of lerna, Daniel, had been encouraging folks to strongly reconsider their use of the legacy package management commands in lerna, and instead leverage their package manager of choice to do what it does best.

我们从远处了解了这一背景,但作为 2022 年该项目的新管理者,我们不想在没有花时间近距离熟悉现实的情况下直接跳入并开始删除功能。现在我们已经积极维护了一段时间,我们完全同意 Daniel 和其他人的观点,即 lerna 中的旧版包管理命令需要退役。

¥We knew about this context from afar, but as new stewards of the project in 2022 we did not want to jump straight in and start removing capabilities without first taking the time to get familiar with the reality up close. Now that we have been actively maintaining for a while, we are in full agreement with Daniel and others that the legacy package management commands in lerna needed to be retired.

通过删除这些在包管理器中本身具有更好替代品的旧版部分,我们和 lerna 社区的其他成员现在可以将精力集中在对 lerna 具有独特价值的事情上(例如但不限于版本控制和发布) ),并让他们成为最好的自己!

¥By removing these legacy pieces which have better alternatives natively in package managers, we and the rest of the lerna community are now freed up to concentrate our efforts on things which are uniquely valuable about lerna (such as, but not limited to, versioning and publishing), and making them the best they can be!

信息

Lerna v7 讨论 也涵盖了相同的背景,如果你有任何具体问题,请加入我们并提供尽可能多的信息!

¥This same context is covered on the Lerna v7 discussion, if you have any specific concerns, please join us there and provide as much information as possible!