Netlify を使って Gatsby で構築したサイト(というかこのブログ)をデプロイしようとしたところ、yarn のパッケージインストールで失敗していました。
4:28:34 PM: yarn install v1.22.4
4:28:35 PM: [1/4] Resolving packages...
4:28:35 PM: [2/4] Fetching packages...
4:29:08 PM: error [email protected]: The engine "node" is incompatible with this module. Expected version ">=14.15.0". Got "12.18.0"
4:29:08 PM: error Found incompatible module.
[email protected] は Netlify で指定されている Node のバージョンとあっておらず、せっかくなので 14.15.0 以上を適用してあげたいところです。
node.js の特定のバージョンを指定する
公式リファレンスを確認すると、
– Set a
https://docs.netlify.com/configure-builds/manage-dependencies/#node-js-and-javascriptNODE_VERSIONenvironment variable.
– Add a.node-versionor.nvmrcfile to the site’s base directory in your repository. This will also tell any other developer using the repository which version of Node.js it depends on.
- NODE_VERSION 環境変数を設定する
- プロジェクトルートディレクトリに
.node-versionもしくは.nvmrcファイルを設置する
とあります。
nodenv を使って Node.js のバージョンを管理している場合、すでに .node-version が設置済かもしれません。nodenv が入っている環境ならプロジェクトごとに Node のバージョンを切り替えることが可能で、 プロジェクトルートにある .node-version のバージョンが適用されます(それ以外はグローバル設定が適用される)。
設置しておくと今後も便利なので、今回はこの .node-version を作成してバージョン指定することにしましょう。
.node-version
16.14.2
これで無事 yarn のパッケージがインストールできるようになりました。
