0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

pnpm で使用するnodeバージョンを固定する&自動インストールする

Posted at
package.json
{
  "name": "web",
  "version": "1.0.0",
  "private": true,
  "engines": {
    "node": "20.10.x",
    "npm": "10.8.x",
    "pnpm": "8.7.x"
  },
  ...
}

このように書くことで、開発に使うnodeのバージョンを指定することができます。
開発者毎でフロントのコードがビルドできる・できないを解消するためによくやるやつですね。

仮にバージョンが異なるnodeでビルドしようとすると、次のようなエラーが出てビルドが早期エラー終了します。

$ v18.17.0
v18.17.0

$ pnpm dev

Your Node version is incompatible with "/Users/lapis/Projects/web".

Expected version: 20.10.x
Got: v18.17.0

This is happening because the package's manifest has an engines.node field specified.
To fix this issue, install the required Node version.
lapis@-MacBook-Air web %

ここからさらに、.npmrcに設定を追加することで環境を改善できます。

.npmrc
engine-strict=true
use-node-version=20.10.0

pnpm will automatically install the specified version of Node.js and use it for running pnpm run commands or the pnpm node command.
...
This setting works only in a .npmrc file that is in the root of your workspace.
公式npmrc

これにより、pnpm devコマンドを実行した場合に、自動でpnpmがnodeを入れて使ってくれるようになります。

*pnpmコマンド実行時に指定のnodeを使い、それ以外のケースでは規定のnodeバージョンを使うようにしてくれます。

// プロジェクトのディレクトリ内でnode,pnpmコマンドでバージョン確認すると、バージョンが異なる。(use-node-version設定を外すとバージョンが同じになるのを確認できる)

$ node --version
v18.17.0

$ pnpm node
Welcome to Node.js v20.18.0
Type ".help" for more information.
>

pnpmを使う利点の1つなので、手隙のときに対応すると仲間に喜ばれるかもしれませんね。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?