Skip to main content

常见问题

¥Frequently asked questions

本文档是一项正在进行的工作。

¥This document is a work in progress.

如何将第三方依赖添加到我的 Lerna 存储库?

¥How do I add a third party dependency to my Lerna repository?

Lerna 不负责添加或链接你的依赖,这是你选择的包管理器(npm/yarn/pnpm)最擅长的。通过使用包管理器的 workspaces 功能,本地包的所有链接都将根据你在 package.json 文件中设置的关系自动进行。

¥Lerna is not responsible for adding or linking your dependencies, that is what your package manager of choice (npm/yarn/pnpm) is best at. By using the workspaces feature of your package manager, all the linking of local packages will happen automatically based on the relationships you set up in your package.json files.

你可以在此处查看包管理器的 workspaces 文档:

¥You can check out the workspaces documentation for your package manager here:

注意:由于 lerna 本身早于包管理器中的 workspaces 功能,因此历史上有一些命令(addbootstraplink)试图填补这一空白。幸运的是,不再需要这些命令,lerna 可以专注于它最擅长的事情,并让包管理器做同样的事情。

¥NOTE: Because lerna itself predates the workspaces feature in package managers, there was historically a few commands (add, bootstrap and link) that attempted to fill that gap. Fortunately these commands are no longer needed and lerna can focus on what it does best and let the package managers does the same.

lerna 仓库中的新软件包

¥New packages within the lerna repo

你可以使用 lerna create 命令在 lerna 存储库中创建新包。

¥You can use the lerna create command to create new packages within your lerna repo.

lerna create <packageName>

请参阅 创建文档 了解更多选项。

¥See the create docs for more options.

如果你不想使用 lerna create,那么你仍然可以通过在 packages 文件夹的子目录中运行 npm init 来手动创建包,Lerna 会自动检测到它。

¥If you don't want to use lerna create, then you can still manually create a package by running npm init within a subdirectory of the packages folder and Lerna will automatically detect it.

现有包

¥Existing packages

你可以使用 lerna import <package> 将现有包传输到你的 Lerna 存储库中;该命令将保留提交历史记录。

¥You can use lerna import <package> to transfer an existing package into your Lerna repository; this command will preserve the commit history.

lerna import <package> 采用本地路径而不是 URL。在这种情况下,你需要在文件系统上拥有你想要链接到的存储库。

¥lerna import <package> takes a local path rather than a URL. In this case you will need to have the repo you wish to link to on your file system.

如果 publish 失败,如何重试发布?

¥How do I retry publishing if publish fails?

如果某些包已成功发布而其他包未成功发布,则 lernapublish 可能使存储库处于与某些更改的文件不一致的状态。要从中恢复,请重置失败运行中的所有无关本地更改,以返回到干净的工作树。然后,重试相同的 lerna 发布命令。Lerna 将尝试再次发布所有包,但会识别那些已发布的包并跳过它们并触发警告。

¥In the case that some packages were successfully published and others were not, lerna publish may have left the repository in an inconsistent state with some changed files. To recover from this, reset any extraneous local changes from the failed run to get back to a clean working tree. Then, retry the same lerna publish command. Lerna will attempt to publish all of the packages again, but will recognize those that have already been published and skip over them with a warning.

如果你使用不带位置参数的 lernapublish 命令来为包选择新版本,那么你可以运行 lernapublishfrom-git 来重试发布相同的已标记版本,而不必在重试时再次更改版本。

¥If you used the lerna publish command without positional arguments to select a new version for the packages, then you can run lerna publish from-git to retry publishing that same already-tagged version instead of having to bump the version again while retrying.

Lerna 如何检测包?

¥How does Lerna detect packages?

默认情况下,对于 npmyarn,lerna 使用 package.json 中配置的 workspaces 属性来了解要操作哪些包。有关此属性的详细信息,请参阅 npm 文档Yarn 文档

¥By default, for npm and yarn, lerna uses the configured workspaces property in package.json to know what packages to operate on. For details on this property, see the npm documentation or the yarn documentation.

如果你使用的是 pnpm,则可能已在 lerna.json 中将 npmClient 设置为 pnpm。在这种情况下,Lerna 将使用 pnpm-workspace.yaml 中的 packages 属性来了解要操作哪些包。有关此属性的详细信息,请参阅 pnpm 文档

¥If you are using pnpm, you might have set npmClient to pnpm in lerna.json. In this case, Lerna will use the packages property in pnpm-workspace.yaml to know what packages to operate on. For details on this property, see the pnpm documentation.

如果你希望 lerna 专注于存储库中的特定软件包子集,你可以利用 lerna.json 中的 packages 属性来搜索软件包。

¥If you want lerna to focus on a particular subset of packages in your repo, you can leverage the packages property in lerna.json to search for packages.