pnpm link

Aliases: ln

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

pnpm link <dir>

Pengaturan#

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.

For example, if you are inside ~/projects/foo and you execute pnpm link ../bar, then a link to bar will be created in foo/node_modules/bar.

If the package is not declared in the package.json of the current project yet, a link: dependency is added to it. The overrides entry for the link is written to the root pnpm-workspace.yaml, so the link applies throughout the workspace.

Breaking changes in v11

pnpm link no longer resolves packages from the global store. Only relative or absolute paths are accepted (use pnpm link ./foo instead of pnpm link foo).

pnpm link --global has been removed. To register a local package's bins globally, use pnpm add -g . instead.

pnpm link with no arguments has been removed. Always pass an explicit path.

Use cases#

Mengganti paket yang terinstal dengan versi lokalnya#

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

Tambahkan biner secara global#

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

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

When you use pnpm link, the linked package is symlinked from the source code. Anda dapat mengubah kode sumber paket yang tertaut, dan perubahannya akan langsung terlihat di proyek Anda. Dengan metode ini pnpm tidak akan menginstal dependensi dari paket tertaut, Anda harus menginstalnya secara manual di kode sumber. 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.

info

When dealing with peer dependencies it is recommended to use the file: protocol. Adalah lebih baik untuk menyelesaikan ketergantungan peer melalui depedensi proyek, agar memastikan bahwa depedensi yang tertaut telah dengan benar menggunakan versi dari dependensi yang ditetapkan dalam proyek utama Anda, sehingga memiliki perilaku yang lebih konsisten dan sesuai harapan.

Fitur pnpm link file: Protocol
Symlink/Hard-link Symlink Hard-link
Mencerminkan modifikasi kode sumber Ya Ya
Menginstal dependensi paket tertaut Tidak (diperlukan instalasi manual) Yes (overrides node_modules of the linked package)
Gunakan manajer paket yang berbeda untuk depedensi ini Possible (e.g., use npm for linked pkg) Tidak, ia akan menggunakan pnpm