Passer au contenu principal
Version : 10.x

pnpm link

Aliases: ln

Rend le paquet local courant accessible depuis le système, ou depuis un autre emplacement.

pnpm link <dir|pkg name>
pnpm link

Options

Links package from <dir> directory to node_modules of package from where you're executing this command.

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.

Links package from location where this command was executed to global node_modules, so it can be referred from another package with pnpm link <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.

Cas d’utilisation

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 # install dependencies of foo
pnpm link # link foo globally
cd ~/projects/my-project
pnpm link foo # link foo to my-project

You can also link a package from a directory to another directory, without using the global node_modules directory:

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

If you are developing a package that has a binary, for example, a CLI tool, you can use pnpm link 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 # link foo globally

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. 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 linkfile: Protocol
Lien symbolique/Lien en durLien symboliqueLien en dur
Reflète les modifications du code sourceOuiOui
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épendancesPossible (e.g., use npm for linked pkg)Non, il utilisera pnpm