📝

[小ネタ] 存在しない Amplify CLI バージョンを Amplify Hosting のパッケージバージョンに指定した場合の挙動

2025/02/18に公開

結論

以下のエラーが発生します。

2025-02-07T10:54:18.635Z [ERROR]: !!! Error patching NPM package '@aws-amplify/cli' to version 12.13.2. Verify the package name and version are correct, and retry the build. For help see our docs: https://docs.aws.amazon.com/amplify/latest/userguide/custom-build-image.html
2025-02-07T10:54:18.732Z [ERROR]: !!! Failed to override package versions

ライブパッケージについて

Customizing the build image - AWS Amplify Hosting

Live package updates enable you to specify the versions of packages and dependencies to use in the Amplify default build image.

Amplify Hosting ではデフォルトのビルドイメージに Amplify CLI などのパッケージがプリンストールされています。
しかし、アプリケーションによっては特定のパッケージバージョンを指定する必要があるケースも考えられます。
Amplify Hosting のライブパッケージではアプリケーションに必要なパッケージバージョンを指定することができるため、アプリケーションとの互換性を保証したいケースなどで使用できます。

ライブパッケージは以下の手順で編集可能です。

  1. Amplify コンソールで対象のアプリケーションを選択
  2. ビルドの設定
  3. ビルドイメージの設定
    1. ライブパッケージの更新

試してみた

前提として、CodeCommit に index.html のみをプッシュしてあります。

まずは Amplify CLI の latest で試してみます。

問題なくビルド、デプロイされました。

Amplify CLI のバージョンを存在しないバージョンに変更してみます。
Amplify CLI のバージョンは GitHub の Releases から確認できます。
Releases · aws-amplify/amplify-cli

今回は 12.13.2 という存在しないバージョンを指定してみました。

冒頭のエラーにより、ビルドが失敗しました。

Error patching NPM package '@aws-amplify/cli' to version 12.13.2. Verify the package name and version are correct, and retry the build.

Amplify CLI のバージョン 12.13.2 を解決できなかったため、バージョンを再確認する必要がある旨のメッセージです。

npm install での結果

npm install で存在しない Amplify CLI バージョンをインストールしようとした場合には以下のメッセージが表示されました。

$ npm install -g @aws-amplify/cli@12.13.2
npm error code ETARGET
npm error notarget No matching version found for @aws-amplify/cli@12.13.2.
npm error notarget In most cases you or one of your dependencies are requesting
npm error notarget a package version that doesn't exist.

存在しない Node.js バージョンを指定した場合の結果

Node.js — Node.js リリース
ライブパッケージで Node.js の 22.13.2 という存在しないバージョンをしたいした場合、ビルドで以下のエラーが発生しました。

2025-02-07T11:11:26.889Z [ERROR]: ! Node version not available for installation. Verify that the version is correct, and retry the build. For help see our docs: https://docs.aws.amazon.com/amplify/latest/userguide/custom-build-image.html
2025-02-07T11:11:27.003Z [ERROR]: !!! Failed to override package versions

latest を指定する際の注意点

Amplify Hosting で設定変更をしていないにもかかわらずビルドエラーが発生するようになったので解決策を教えてください | DevelopersIO

Amplify Hosting ではビルド時にデフォルトで Amplify CLI の最新版 (latest) が使用されます。
しかし、Amplify CLI の最新バージョンでは、ユーザーが設定変更をしていなくてもビルドエラーなどが発生する可能性があります。

latest にもバグが含まれている可能性はあるため、本番環境ではバージョンを固定することもご検討ください。

まとめ

今回は存在しない Amplify CLI バージョンを Amplify Hosting のパッケージバージョンに指定した場合の挙動を紹介しました。
どなたかの参考になれば幸いです。

参考資料

Discussion