Langsung ke konten utama
Versi: 9.x

pnpm link

Aliases: ln

Menjadikan paket lokal saat ini dapat diakses di seluruh sistem, atau di lokasi lain.

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

Pengaturan

--dir &lt;dir>, -C

  • Default: Current working directory
  • Type: Path string

Changes the link location to <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 execute pnpm link --dir ../bar, then foo will be linked to 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.

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

Menggunakan Kasus

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, while the package.json won't be modified.

cd ~/projects/foo
pnpm install # instal dependensi foo
pnpm link --global # hubungkan foo secara global
cd ~/projects/my-project
pnpm link --global foo # hubungkan foo ke proyek-saya

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

Tambahkan biner secara global

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

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

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.

Fiturpnpm linkfile: Protocol
Symlink/Hard-linkSymlinkHard-link
Mencerminkan modifikasi kode sumberYaYa
Menginstal dependensi paket tertautTidak (diperlukan instalasi manual)Yes (overrides node_modules of the linked package)
Gunakan manajer paket yang berbeda untuk depedensi iniPossible (e.g., use npm for linked pkg)Tidak, ia akan menggunakan pnpm