AWS Amplify に vite React で構築したアプリをデプロイしようとしたのですが、プロジェクトで使っている Node.js を v18.16.0 に固定していてビルドエラーになってしまったので Amplify で Node.js v18系を使えるようにしたまでのメモ
結論 AWS Linux:2 の場合は用意されている Node.js v18.13.0 を使うのが良さそう
amplify.yml
version: 1 frontend: phases: preBuild: commands: - nvm use 18 - node -v - npm ci build: commands: - npm run build
Amplify で使う Node.js のバージョンを変更する
package.json
の engines プロパティと --engine-strict
を使って npm install できる Node.js のバージョンを固定していると Amplify の環境とバージョンが合わずデプロイがコケる
package.json
{ … "engines": { "node": "^18.16.0", } }
.npmrc
engine-strict=true
上記の設定の場合 Node.js v18.16 系以外で npm install しようとするとエラーになるので AWS Amplify のデプロイで Node.js のバージョンが合わずエラーになる
[WARNING]: ERR! code EBADENGINE npm ERR! notsup Required: {"node":"^18.16.0"} npm ERR! notsup Actual: {"npm":"8.19.3","node":"v16.19.0"} [ERROR]: !!! Build failed
Amplify では Node.js v16.19.0 が動作していました (2023-05-11 現在)
Node v18.16.0 をインストールする
If you need any other minor/patch version of Node 18, please install it from source like so:
nvm install -s 18.x.xcf. Fix support for node 18 · Issue #3109 · aws-amplify/amplify-hosting · GitHub
Amplify のイメージでは nvm が使われているので、nvm を使って Node.js をインストールできるっぽい
amplify.yml
version: 1 frontend: phases: preBuild: commands: - nvm install 18.16.0 - node -v - npm ci build: commands: - npm run build
🙅 AMplify で Node v18.16.0 のインストールはエラーになる
上記の設定で Node.js のバージョンをプロジェクトに合わせようとしたのですが Amplify のビルドでエラーになってしまいました
[INFO]: Downloading and installing node v18.16.0... # 略 [INFO]: Now using node v18.16.0 (npm v) [INFO]: npm install -g --quiet yarn@1.22.0 [WARNING]: node: /lib64/libm.so.6: version `GLIBC_2.27' not found (required by node) node: /lib64/libc.so.6: version `GLIBC_2.28' not found (required by node) [WARNING]: Failed installing default packages. Please check if your default-packages file or a package in it has problems! [ERROR]: !!! Build failed
yarn のインストール時でコケてしまっている…
If you have to use Node.js 18 for your build (ie. Gatsby 5) on AWS Amplify, you may have seen the error
GLIBC_2.27 not found
. By default AWS Amplify uses Amazon Linux:2 as its build image and it comes with GLIBC 2.26. However, building frontend apps that require Node.js 18 will require GLIBC 2.27 or 2.28.
cf. AWS Amplify - Specified Node 18 but GLIBC_2.27 or GLIBC_2.28 not found on build - Frontend Club
Amplify コンテナの Amazon Linux:2 イメージが原因っぽい
glibc 2.27+ on Amazon Linux 2 | AWS re:Post によると Amazon Linux 2022 になれば glibc のバージョンが上がり使えるようになるっぽい
🙆 インストールはエラーになるが nvm use 18
なら問題なかった
Fix support for node 18 · Issue #3109 · aws-amplify/amplify-hosting · GitHub こちらの issue のコメントに Node.js v18.13.0 を nvm use 18
で使えるようにしたとあり、試してみた所問題がなかった。
先の問題は新しく Node.js v18 系をインストールしようとした際に発生してしまうものだった模様
amplify.yml
version: 1 frontend: phases: preBuild: commands: - - nvm install 18.16.0 + - nvm use 18 - node -v - npm ci build: commands: - npm run build
👇 Amplify build
[INFO]: Now using node v18.13.0 (npm v8.19.3) [INFO]: # Executing command: node -v [INFO]: v18.13.0 [INFO]: # Executing command: npm ci … [INFO]: # Environment caching completed
プロジェクトの Node.js のバージョンを v18.13.0 に変更する
cf. フロントエンド開発環境: チーム開発するリポジトリで node のバージョンを揃えたい - かもメモ
package.json
{ "engines": { // npm install できる Node.js のバージョン - "node": "^18.16.0" + "node": "^18.13.0" }, "volta": { - "node": "18.16.0", + "node": "18.13.0", } }
.node-version
- 18.16.0 + 18.13.0
先に LTS な Node.js v18.16 で作っていたプロジェクトを AWS Amplify でデプロイしたかっただけなんだけど Amplify で Node v18系使うのにハマると思ってなかった…
おわり
[参考]
- フロントエンドエンジニアに捧げるAWS Amplify Console | DevelopersIO
- Tutorial: Setting Up Node.js on an Amazon EC2 Instance - AWS SDK for JavaScript
- Fix support for node 18 · Issue #3109 · aws-amplify/amplify-hosting · GitHub
- Amplify Consoleのデフォルトビルドイメージで動作するNode.jsのバージョンを変更する | DevelopersIO
- AWS Amplify - Specified Node 18 but GLIBC_2.27 or GLIBC_2.28 not found on build - Frontend Club
- Amplify Consoleでのnodeのバージョン指定方法、LTSを使うには - Qiita
- Amplify Consoleのnode、yarnをバージョンアップする(Quasarアプリをビルドする) - Qiita