pnpm link
Aliases: ln
Links a local package to the current project's node_modules.
pnpm link [--hard] <dir>...
A quoted path beginning with ~/ is relative to your home directory. If the target's manifest omits name, pnpm uses its directory name.
Utilisation#
pnpm link <dir>...#
Links the package from each <dir> directory to node_modules of the package from where you're executing this command. <dir> must be a relative or absolute path. A relative path is resolved against the directory you run the command in. Passing a package name, or no argument, fails with ERR_PNPM_LINK_BAD_PARAMS.
For example, if you are inside
~/projects/fooand you executepnpm link ../bar, then a link tobarwill be created infoo/node_modules/bar.
pnpm link writes two entries:
- a
link:dependency inpackage.json, if the package is not declared there yet; - an
overridesentry<name>: link:<path>in the rootpnpm-workspace.yaml, so the link applies throughout the workspace.
pnpm warns about peer dependencies of the linked package. pnpm unlink removes the overrides.
Options#
--hard#
Added in: v12.23.0
Writes both entries with the file: protocol instead of link:. pnpm then installs the package like any file: dependency. It hard links the package's files into node_modules and resolves its dependencies and peer dependencies from your project. See the difference between pnpm link and the file: protocol.
pnpm link --hard ../foo
Edits to existing files of the linked package show up in your project while the files stay hard linked. Run pnpm install to pick up new or deleted files. Also run it after edits if pnpm had to copy the files, for example because the package is on another drive.
--hard refuses a directory whose name ends in .tgz, .tar.gz, or .tar, because a file: specifier with that ending names a tarball.
If the linked directory is a project of the same workspace, pnpm may still symlink it when dedupeInjectedDeps is enabled.
Use cases#
Remplacer le paquet installé par une version locale de celui-ci#
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 # installer les dépendances de foo
cd ~/projects/my-project
pnpm link ~/projects/foo # lier foo à my-project
Ajouter un binaire globalement#
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.
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. Vous pouvez modifier le code source du paquet lié et les modifications seront appliquées dans votre projet. Avec cette méthode, pnpm n'installera pas les dépendances du paquet lié, vous devrez les installer manuellement dans le code source. 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, for example with pnpm link --hard. Il résout mieux les dépendances entre pairs à partir des dépendances du projet, garantissant que la dépendance liée utilise correctement les versions des dépendances spécifiées dans votre projet principal, conduisant à des comportements plus cohérents et attendus.
| Fonctionnalité | pnpm link |
file: Protocol |
|---|---|---|
| Lien symbolique/Lien en dur | Lien symbolique | Lien en dur |
| Reflète les modifications du code source | Oui | Oui |
| Installe les dépendances du paquet lié | Non (une installation manuelle est requise) | Yes (overrides node_modules of the linked package) |
| Utilisation d'un gestionnaire de paquet différent pour les dépendances | Possible (e.g., use npm for linked pkg) |
Non, il utilisera pnpm |