Langsung ke konten utama
Versi: Selanjutnya

pnpm link

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

Opsi

--dir <dir>, -C

  • Asali: Direktori kerja saat ini
  • Tipe: String jalur lokasi

Mengubah lokasi tautan menjadi <dir>.

Menautkan paket dari folder <dir> ke paket node_modules daro tempat Anda menjalankan perintah ini atau ditentukan melalui opsi --dir.

Contohnya, jika anda berada dalam ~/projects/foo dan anda menjalankan pnpm link --dir ../bar, maka foo akan terhubung ke bar/node_modules/foo.

Menautkan paket dari lokasi tempat perintah ini dijalankan atau ditentukan melalui opsi --dir ke global node_modules, sehingga dapat dirujuk dari paket lain dengan pnpm link --global <pkg>. Juga jika paket memiliki field bin, maka binari paket akan tersedia di seluruh sistem.

Menautkan paket yang ditentukan (<pkg>) dari node_modules global ke paket node_modules di mana perintah ini dieksekusi atau ditentukan melalui opsi --dir.

pnpm link <dir> menghubungkan paket dari <dir> ke node_modules dari paket tempat perintah dijalankan. pnpm link --dir <dir> menghubungkan paket dari tempat kerja saat ini ke <dir>.

# Tempat direktori saat ini adalah foo
pnpm link ../bar

- foo
- node_modules
- bar -> ../../bar
- bar

# Tempat direktori saat ini adalah bar
pnpm link --dir ../foo

- foo
- node_modules
- bar -> ../../bar
- bar

Menggunakan Kasus

Mengganti paket yang terinstal dengan versi lokalnya

Katakanlah anda memiliki proyek dengan menggunakan paket foo. Anda akan mengubah ke foo dan mengujinya di proyek anda. Dalam skenario ini, anda dapat menggunakan pnpm link untuk menghubungkan ke versi lokal dari foo pada proyek anda, sedangkan package.json tidak akan diubah.

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

Add a binary globally

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

info

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.

Fiturpnpm linkfile: Protocol
Symlink/Hard-linkSymlinkHard-link
Reflects source code modificationsYesYes
Installs dependencies of the linked packageNo (manual installation required)Yes (overrides node_modules of the linked package)
Use different package manager for dependencyPossible (e.g., use npm for linked pkg)No, it will use pnpm