Skip to main content
Version: Next

pnpm link

别名:ln

🌐 Aliases: ln

将本地包链接到当前项目的 node_modules

🌐 Links a local package to the current project's node_modules.

pnpm link <dir>

选项

🌐 Options

<dir> 目录中的包链接到你执行此命令的包的 node_modules<dir> 必须是相对或绝对路径。

🌐 Links package from <dir> directory to node_modules of package from where you're executing this command. <dir> must be a relative or absolute path.

例如,如果你在 ~/projects/foo 中并执行 pnpm link ../bar,那么在 foo/node_modules/bar 中将创建一个指向 bar 的链接。

v11 中的重大更改

pnpm link 不再从全局存储解析包。只接受相对路径或绝对路径(使用 pnpm link ./foo 替代 pnpm link foo)。

pnpm link --global 已被移除。要全局注册本地包的可执行文件,请改用 pnpm add -g .

pnpm link 无参数已被移除。请始终传递明确的路径。

用例

🌐 Use Cases

将已安装的软件包替换为其本地版本

🌐 Replace an installed package with a local version of it

假设你有一个使用 foo 包的项目。你想对 foo 进行更改并在你的项目中测试它们。在这种情况下,你可以使用 pnpm link 将本地版本的 foo 链接到你的项目中:

🌐 Let's say you have a project that uses foo package. You want to make changes to foo and test them in your project. In this scenario, you can use pnpm link to link the local version of foo to your project:

cd ~/projects/foo
pnpm install # install dependencies of foo
cd ~/projects/my-project
pnpm link ~/projects/foo # link foo to my-project

全局添加二进制文件

🌐 Add a binary globally

要使本地包的二进制文件在整个系统中可用,请改用 pnpm add -g .

🌐 To make a local package's binaries available system-wide, use pnpm add -g . instead:

cd ~/projects/foo
pnpm install # install dependencies of foo
pnpm add -g . # register foo's bins globally

请记住,只有当包的 package.json 中包含 bin 字段时,该二进制文件才可用。

🌐 Remember that the binary will be available only if the package has a bin field in its package.json.

🌐 What's the difference between pnpm link and using the file: protocol?

当你使用 pnpm link 时,关联的包会从源代码创建符号链接。你可以修改关联包的源代码,修改将会反映到你的项目中。使用这种方法,pnpm 不会安装关联包的依赖,你需要在源代码中手动安装它们。当你必须为关联包使用特定的包管理器时,这可能会很有用,例如,如果你想为关联包使用 npm,但你的项目使用 pnpm。

🌐 When you use pnpm link, the linked package is symlinked from the source code. You can modify the source code of the linked package, and the changes will be reflected in your project. With this method pnpm will not install the dependencies of the linked package, you will have to install them manually in the source code. This may be useful when you have to use a specific package manager for the linked package, for example, if you want to use npm for the linked package, but pnpm for your project.

当你在 dependencies 中使用 file: 协议时,链接的包会被硬链接到你的项目 node_modules,你可以修改链接包的源代码,修改会反映到你的项目中。使用这种方法,pnpm 还会安装链接包的依赖,覆盖链接包的 node_modules

🌐 When you use the file: protocol in dependencies, the linked package is hard-linked to your project node_modules, you can modify the source code of the linked package, and the changes will be reflected in your project. With this method pnpm will also install the dependencies of the linked package, overriding the node_modules of the linked package.

info

在处理同伴依赖时,建议使用 file: 协议。它可以更好地从项目依赖中解析同伴依赖,确保链接的依赖正确使用主项目中指定的依赖版本,从而带来更一致和预期的行为。

🌐 When dealing with peer dependencies it is recommended to use the file: protocol. It better resolves the peer dependencies from the project dependencies, ensuring that the linked dependency correctly uses the versions of the dependencies specified in your main project, leading to more consistent and expected behaviors.

特性pnpm linkfile: 协议
符号链接/硬链接符号链接硬链接
反映源代码修改
安装被链接包的依赖否(需要手动安装)是(会覆盖被链接包的 node_modules
为依赖使用不同的包管理器可行(例如,为被链接包使用 npm否,它会使用 pnpm