メインコンテンツまでスキップ
Version: Next

pnpm link

別名:ln

Links a local package to the current project's node_modules.

pnpm link <dir>

引数

Links package from <dir> directory to node_modules of package from where you're executing this command. <dir> must be a relative or absolute path.

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.

ユースケース

インストールされているパッケージをローカル バージョンに置き換える

foo パッケージを使用するプロジェクトがあるとします。 foo に変更を加え、プロジェクトでテストしたいと考えています。 In this scenario, you can use pnpm link to link the local version of foo to your project:

cd ~/projects/foo
pnpm install # foo の依存関係をインストールする
cd ~/projects/my-project
pnpm link ~/projects/foo # foo を my-project にリンクする

バイナリをグローバルに追加する

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.

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

情報

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.

機能pnpm linkfile: プロトコル
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