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つなので、手隙のときに対応すると仲間に喜ばれるかもしれませんね。