Saltar al contenido principal
Version: Próxima versión

pnpm link

Alias: ln

Hace que el paquete local actual sea accesible en todo el sistema, o en otra ubicación.

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

Opciones

--dir <dir>, -C

  • Predeterminado: Directorio de trabajo actual
  • Tipo: Ruta (como string)

Cambia la ubicación del enlace a <dir>.

Enlaza el paquete desde la carpeta <dir> a los node_modules del paquete situado en la ruta en la que se esté ejecutando este comando u otra especificada a través de la opción --dir.

Por ejemplo, si está dentro de ~/projects/foo y ejecuta pnpm link --dir ../bar, entonces foo se vinculará a bar/node_modules/foo.

Enlaza el paquete desde la ubicación donde se ejecutó este comando o desde donde se especificó a través de la opción --dir a los node_modules globales, por lo que puede ser referenciado desde otro paquete con pnpm link --global <pkg>. Además, si el paquete tiene un campo bin, los archivos binarios del paquete estarán disponibles en todo el sistema.

Enlaza el paquete especificado (<pkg>) de los node_modules globales a los node_modules del paquete desde donde se ejecutó este comando o se especificó usando --dir.

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

Use Cases

Reemplazar un paquete instalado con una versión local del mismo

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

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

Añadir un binario globalmente

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.

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. :::
Característicapnpm linkProtocolo file:
Enlace simbólico/Enlace duroEnlace simbólicoEnlace duro
Refleja las modificaciones del código fuente
Instala dependencias del paquete vinculadoNo (requiere instalación manual)Sí (sobreescribe el node_modules del paquete vinculado)
Usa un administrador de paquetes diferente para la dependenciaPosible (p. ej., use npm para paquete vinculado)No, usará pnpm