pnpm link
别名: ln
使当前本地包可在系统范围内或其他位置访问。
pnpm link <dir>
pnpm link --global
pnpm link --global <pkg>
配置项
--dir <dir>, -C
- 默认值:当前工作目录
- 类型:路径
将 link 位置改为 <dir>
.
pnpm link <dir>
从执行此命令的路径或通过 <dir>
指定的文件夹,链接package
到node_modules
中。
例如,如果你正处于
~/projects/foo
目录下并执行了pnpm link --dir ../bar
,则foo
将会被链接到bar/node_modules/foo
。
pnpm link --global
将此命令在执行的位置或通过–dir
选项指定的位置链接到全局的node_modules
,这样它就可以通过pnpm link --global <pkg>
从另一个软件包中引用。 Also if the package has a bin
field, then the package's binaries become available system-wide.
pnpm link --global <pkg>
将指定的包(<pkg>
)从全局 node_modules
链接到 package
的 node_modules
,从该 package
中执行或通过 --dir
选项指定。
Difference between pnpm link <dir>
and pnpm link --dir <dir>
pnpm link <dir>
links the package from <dir>
to the node_modules
of the package where the command was executed. pnpm link --dir <dir>
links the package from the current working directory to <dir>
.
# The current directory is foo
pnpm link ../bar
- foo
- node_modules
- bar -> ../../bar
- bar
# The current directory is bar
pnpm link --dir ../foo
- foo
- node_modules
- bar -> ../../bar
- bar
Use Cases
将已安装的软件包替换为本地版本
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, while the package.json
won't be modified.
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
You can also link a package from a directory to another directory, without using the global node_modules
folder:
cd ~/projects/foo
pnpm install # install dependencies of foo
cd ~/projects/my-project
pnpm link ~/projects/foo # link foo to my-project
全局添加二进制文件
If you are developing a package that has a binary, for example, a CLI tool, you can use pnpm link --global
to make the binary available system-wide. 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.
Remember that the binary will be available only if the package has a bin
field in its package.json
.
cd ~/projects/foo
pnpm install # install dependencies of foo
pnpm link --global # link foo globally
What's the difference between pnpm link
and using the file:
protocol?
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.
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 link | file: Protocol |
---|---|---|
Symlink/Hard-link | Symlink | Hard-link |
Reflects source code modifications | Yes | Yes |
Installs dependencies of the linked package | No (manual installation required) | Yes (overrides node_modules of the linked package) |
Use different package manager for dependency | Possible (e.g., use npm for linked pkg) | No, it will use pnpm |