pnpm link
Aliases: ln
使当前本地包可在系统范围内或其他位置访问。
pnpm link <dir>
pnpm link --global
pnpm link --global <pkg>
配置项
--dir <dir>, -C
- Default: Current working directory
- Type: Path string
Changes the link location to <dir>
.
pnpm link <dir>
Links package from <dir>
folder to node_modules of package from where you're executing this command or specified via --dir
option.
For example, if you are inside
~/projects/foo
and you executepnpm link ../bar
, then a link tobar
will be created infoo/node_modules/bar
.
pnpm link --global
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.
pnpm link --global <pkg>
Links the specified package (<pkg>
) from global node_modules
to the node_modules
of package from where this command was executed or specified via --dir
option.
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
用例
将已安装的软件包替换为本地版本
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. 您可以修改链接包的源代码,所做的更改将反映在您的项目中。 使用这种方法 pnpm 将不会安装链接包的依赖项,您必须在源代码中手动安装它们。 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.
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. 它更好地解决了项目依赖项中的对等依赖项,确保链接的依赖项正确使用主项目中指定的依赖项版本,从而实现更加一致和预期的行为。
功能 | pnpm link | file: Protocol |
---|---|---|
符号链接/硬链接 | 符号链接 | 硬链接 |
反映源代码修改 | 是 | 是 |
安装链接包的依赖项 | 否(需要手动安装) | Yes (overrides node_modules of the linked package) |
使用不同的包管理器来实现依赖 | Possible (e.g., use npm for linked pkg) | 不可以,它将使用 pnpm |