pnpm link
别名:ln
¥Aliases: ln
使当前本地包可在系统范围内或在其他位置访问。
¥Makes the current local package accessible system-wide, or in another location.
pnpm link <dir>
pnpm link --global
pnpm link --global <pkg>
选项
¥Options
--dir <dir>, -C
-
默认:当前工作目录
¥Default: Current working directory
-
类型:路径字符串
¥Type: Path string
将链接位置更改为 <dir>
。
¥Changes the link location to <dir>
.
pnpm link <dir>
将包从 <dir>
文件夹链接到你执行此命令或通过 --dir
选项指定的包的 node_modules。
¥Links package from <dir>
folder to node_modules of package from where you're executing this command or specified via --dir
option.
例如,如果你在
~/projects/foo
里面,执行pnpm link --dir ../bar
,那么foo
就会链接到bar/node_modules/foo
。¥For example, if you are inside
~/projects/foo
and you executepnpm link --dir ../bar
, thenfoo
will be linked tobar/node_modules/foo
.
pnpm link --global
将包从执行此命令或通过 --dir
选项指定的位置链接到全局 node_modules
,因此可以从具有 pnpm link --global <pkg>
的另一个包引用它。此外,如果包具有 bin
字段,则包的二进制文件在系统范围内可用。
¥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>
将指定包 (<pkg>
) 从全局 node_modules
链接到执行此命令或通过 --dir
选项指定的包的 node_modules
。
¥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.
pnpm link <dir>
和 pnpm link --dir <dir>
之间的区别
¥Difference between pnpm link <dir>
and pnpm link --dir <dir>
pnpm link <dir>
将包从 <dir>
链接到执行命令的包的 node_modules
。pnpm link --dir <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
将已安装的软件包替换为其本地版本
¥Replace an installed package with a local version of it
假设你有一个使用 foo
包的项目。你想要对 foo
进行更改并在你的项目中测试它们。在这种情况下,你可以使用 pnpm link
将本地版本的 foo
链接到你的项目,而 package.json
不会被修改。
¥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
你还可以将包从一个目录链接到另一个目录,而不使用全局 node_modules
文件夹:
¥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
全局添加二进制文件
¥Add a binary globally
如果你正在开发包含二进制文件的软件包(例如 CLI 工具),则可以使用 pnpm link --global
使二进制文件在系统范围内可用。这与使用 pnpm install -g foo
相同,但它将使用本地版本的 foo
,而不是从注册表下载。
¥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.
请记住,仅当包的 package.json
中有 bin
字段时,二进制文件才可用。
¥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
pnpm link
和使用 file:
协议有什么区别?
¥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.
处理对等依赖时,建议使用 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 link | file: 协议 |
---|---|---|
符号链接/硬链接 | 符号链接 | 硬链接 |
反映源代码修改 | 是的 | 是的 |
安装链接包的依赖 | 否(需要手动安装) | 是(覆盖链接包的 node_modules ) |
使用不同的包管理器来实现依赖 | 可能(例如,使用 npm 作为链接的 pkg) | 不,它将使用 pnpm |