跳到主内容
版本:8.x

pnpm link

别名: ln

使当前本地包可在系统范围内或其他位置访问。

pnpm link <dir>
pnpm link --global
pnpm link --global <pkg>

配置项

--dir <dir>, -C

  • 默认值:当前工作目录
  • 类型:路径

将 link 位置改为 <dir>.

从执行此命令的路径或通过 <dir> 指定的文件夹,链接packagenode_modules中。

例如,如果你正处于 ~/projects/foo 目录下并执行了 pnpm link --dir ../bar ,则 foo 将会被链接到 bar/node_modules/foo

Links package from location where this command was executed or specified via --dir option to global node_modules, so it can be referred from another package with pnpm link --global <pkg>. Also if the package has a bin field, then the package's binaries become available system-wide.

将指定的包(<pkg>)从全局 node_modules 链接到 packagenode_modules,从该 package 中执行或通过 --dir 选项指定。

使用案例

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

假设您有一个使用 foo 包的项目。 您想要对 foo 进行更改并在项目中测试它们。 在这种情况下,您可以使用 pnpm link 将本地版本的 foo 链接到您的项目,而 package.json 不会被修改。

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

您也可以将包从一个目录链接到另一个目录,而不是使用全局 node_modules 文件夹:

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

全局添加二进制文件

如果您正在开发一个包含二进制文件的软件包(例如命令行工具),则可以使用 pnpm link --global 使二进制文件在系统范围内可用。 This is the same as using pnpm install -g foo, but it will use the local version of foo instead of downloading it from the registry.

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

cd ~/projects/foo
pnpm install # install dependencies of foo
pnpm link --global # link foo globally

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 usefull 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.

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.

提示

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: Protocol
Symlink/Hard-linkSymlinkHard-link
Reflects source code modificationsYesYes
Installs dependencies of the linked packageNo (manual installation required)Yes (overrides node_modules of the linked package)
Use different package manager for dependencyPossible (e.g., use npm for linked pkg)No, it will use pnpm