4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Amplify Consoleでのnodeのバージョン指定方法、LTSを使うには

Last updated at Posted at 2022-03-29

Amplify consoleで npm ビルドをする、その時のnodeのバージョンについて

  • デフォルトNodeバージョンが14.18.0 だったり、14.18.1 にだったり。
  • Amplify consoleでデプロイするようにすると、その時にセットアップされた nodeのバージョンがデフォルトで使われるような動きにも見えるが、そうでもない動き。
  • どのバージョンでビルドしているのか、バージョン変えるにはどうするのかを確認・変更方法です

この記事は、2022-03-28, 3/29時点の情報です。

Nodeのバージョンを確認する方法

  • ビルドはデフォルトのままであれば、Amazon Linux2イメージが使われる
  • ビルドのamplify.yml の設定でデフォルトでは Node12,14,16あたりは入っているがバージョンはAWS側で用意されたバージョンとなる
  • AWSマネジメントコンソールの Amplifyのアプリ設定にある「ビルド設定」のデフォルト値の確認
  • アプリケーション構築の仕様の「編集」より
version: 1
frontend:
  phases:
    preBuild:
      commands:
        - npm ci
    build:
      commands:
        - npm run build
  artifacts:
    baseDirectory: build
    files:
      - '**/*'
  cache:
    paths:
      - node_modules/**/*
  • 以前は、 npm install の記載があったが、2021年11,12月頃に npm ciがデフォルトに変わった

amplify.yml で nodeのバージョンを確認するためにコマンドを追加

  • preBuildフェーズで、node -v でビルドログにバージョンを表示させる
frontend:
  phases:
    preBuild:
      commands:
        - node -v
        - npm ci
    build:
      commands:
        - npm run build

  • ビルドログ いつのまにか 14.19.0 になっている( 14.18.0から変わった)
2022-03-29T06:33:36.697Z [INFO]: # Retrieving cache...
2022-03-29T06:33:39.264Z [INFO]: # Extracting cache...
2022-03-29T06:33:42.605Z [INFO]: # Extraction completed
                                 # Starting phase: preBuild
                                 # Executing command: node -v
2022-03-29T06:34:09.783Z [INFO]: v14.19.0
2022-03-29T06:34:09.783Z [INFO]: # Executing command: npm ci
2022-03-29T06:34:10.909Z [WARNING]: npm
2022-03-29T06:34:10.910Z [WARNING]: WARN prepare removing existing node_modules/ before installation

node 14系の最新を入れる

  • ビルド設定
version: 1
frontend:
  phases:
    preBuild:
      commands:
        - nvm install 14
        - node -v
        - npm ci
    build:
      commands:
        - npm run build
  • ビルドログ、v14.19.1がインストールされている
2022-03-29T08:43:07.995Z [INFO]: # Extraction completed
                                 # Starting phase: preBuild
                                 # Executing command: nvm install 14
2022-03-29T08:43:34.299Z [INFO]: Downloading and installing node v14.19.1...
2022-03-29T08:43:34.420Z [WARNING]: Downloading https://nodejs.org/dist/v14.19.1/node-v14.19.1-linux-x64.tar.gz...
2022-03-29T08:43:34.539Z [WARNING]: #####                                                                      7.7%
2022-03-29T08:43:34.639Z [WARNING]: ####################################################                      72.4%
2022-03-29T08:43:34.674Z [WARNING]: ##############################
2022-03-29T08:43:34.674Z [WARNING]: ########################################## 100.0%
2022-03-29T08:43:34.691Z [WARNING]: Computing checksum with sha256sum
2022-03-29T08:43:34.779Z [WARNING]: Checksums matched!
2022-03-29T08:43:35.841Z [INFO]: Now using node v14.19.1 (npm v6.14.16)
2022-03-29T08:43:35.872Z [INFO]: Installing default global packages from /root/.nvm/default-packages...
                                 npm install -g --quiet yarn@1.22.0
2022-03-29T08:43:37.346Z [INFO]: /root/.nvm/versions/node/v14.19.1/bin/yarnpkg -> /root/.nvm/versions/node/v14.19.1/lib/node_modules/yarn/bin/yarn.js
2022-03-29T08:43:37.347Z [INFO]: /root/.nvm/versions/node/v14.19.1/bin/yarn -> /root/.nvm/versions/node/v14.19.1/lib/node_modules/yarn/bin/yarn.js
2022-03-29T08:43:37.348Z [INFO]: + yarn@1.22.0
                                 added 1 package in 1.107s
2022-03-29T08:43:37.392Z [INFO]: # Executing command: node -v
2022-03-29T08:43:37.395Z [INFO]: v14.19.1
2022-03-29T08:43:37.395Z [INFO]: # Executing command: npm ci
2022-03-29T08:43:37.743Z [WARNING]: npm
2022-03-29T08:43:37.743Z [WARNING]: WARN prepare removing existing node_modules/ before installation
2022-03-29T08:43:50.706Z [INFO]: > core-js@3.20.0 postinstall /codebuild/output/src091330464/src/sugimoto-apptest/node_modules/core-js
                                 > node -e "try{require('./postinstall')}catch(e){}"
2022-03-29T08:43:50.751Z [INFO]: [96mThank you for using core-js ([94m https://github.com/zloirock/core-js [96m) for polyfilling JavaScript standard library![0m
                                 [96mThe project needs your help! Please consider supporting of core-js:[0m
                                 [96m>[94m https://opencollective.com/core-js [0m
                                 [96m>[94m https://patreon.com/zloirock [0m
                                 [96m>[94m https://paypal.me/zloirock [0m
                                 [96m>[94m bitcoin: bc1qlea7544qtsmj2rayg0lthvza9fau63ux0fstcz [0m
                                 [96mAlso, the author of core-js ([94m https://github.com/zloirock [96m) is looking for a good job -)[0m
2022-03-29T08:43:50.971Z [INFO]: > core-js-pure@3.20.0 postinstall /codebuild/output/src091330464/src/sugimoto-apptest/node_modules/core-js-pure
                                 > node -e "try{require('./postinstall')}catch(e){}"
2022-03-29T08:43:51.123Z [INFO]: added 1403 packages in 13.397s
2022-03-29T08:43:51.137Z [INFO]: # Completed phase: preBuild
                                 # Starting phase: build
                                 # Executing command: npm run build
2022-03-29T08:43:51.305Z [INFO]: > apptest@0.1.0 build /codebuild/output/src091330464/src/sugimoto-apptest
                                 > react-scripts build
2022-03-29T08:43:52.434Z [INFO]: Creating an optimized production build...

Nodeバージョンを指定する(バージョン固定)サンプル

  • ビルド設定
version: 1
frontend:
  phases:
    preBuild:
      commands:
        - nvm install 14.18.3
        - node -v
        - npm ci
    build:
      commands:
        - npm run build
  • ビルドログ
                                 # Executing command: nvm install 14.18.3
2022-03-29T08:48:18.392Z [INFO]: Downloading and installing node v14.18.3...
2022-03-29T08:48:18.517Z [WARNING]: Downloading https://nodejs.org/dist/v14.18.3/node-v14.18.3-linux-x64.tar.gz...
2022-03-29T08:48:18.634Z [WARNING]: ##
2022-03-29T08:48:18.635Z [WARNING]: 4.1%
2022-03-29T08:48:18.735Z [WARNING]: ##################
2022-03-29T08:48:18.736Z [WARNING]: ##############                                          45.8%
2022-03-29T08:48:18.836Z [WARNING]: #####################
2022-03-29T08:48:18.837Z [WARNING]: #############################################        92.3%
2022-03-29T08:48:18.848Z [WARNING]: ################
2022-03-29T08:48:18.849Z [WARNING]: ######################################################## 100.0%
2022-03-29T08:48:18.864Z [WARNING]: Computing checksum with sha256sum
2022-03-29T08:48:18.950Z [WARNING]: Checksums matched!
2022-03-29T08:48:20.027Z [INFO]: Now using node v14.18.3 (npm v6.14.15)
2022-03-29T08:48:20.057Z [INFO]: Installing default global packages from /root/.nvm/default-packages...
2022-03-29T08:48:20.057Z [INFO]: npm install -g --quiet yarn@1.22.0
2022-03-29T08:48:21.666Z [INFO]: /root/.nvm/versions/node/v14.18.3/bin/yarn -> /root/.nvm/versions/node/v14.18.3/lib/node_modules/yarn/bin/yarn.js
2022-03-29T08:48:21.668Z [INFO]: /root/.nvm/versions/node/v14.18.3/bin/yarnpkg -> /root/.nvm/versions/node/v14.18.3/lib/node_modules/yarn/bin/yarn.js
2022-03-29T08:48:21.669Z [INFO]: + yarn@1.22.0
                                 added 1 package in 1.286s
2022-03-29T08:48:21.702Z [INFO]: # Executing command: node -v
2022-03-29T08:48:21.705Z [INFO]: v14.18.3

14.18.3になった

Node16系を使う

  • nvm use $VERSION_NODE_16 を指定する
  • ビルド設定
version: 1
frontend:
  phases:
    preBuild:
      commands:
        - nvm use $VERSION_NODE_16
        - node -v
        - npm ci
    build:
      commands:
        - npm run build
  artifacts:
    baseDirectory: build
    files:
      - '**/*'
  cache:
    paths:
      - node_modules/**/*
  • ビルドログ
                                 # Starting phase: preBuild
                                 # Executing command: nvm use 16
2022-03-29T09:05:08.405Z [INFO]: Now using node v16.13.2 (npm v8.1.2)
2022-03-29T09:05:08.407Z [INFO]: # Executing command: node -v
2022-03-29T09:05:08.409Z [INFO]: v16.13.2
2022-03-29T09:05:08.410Z [INFO]: # Executing command: npm ci

16.13.2 が使われる(Amazon Linux2のDockerイメージでは、2022/3/28時点では 16.13.2が入っていることになる)

Amazon Linux2 Docker イメージ情報

  • Node14がデフォルトになっている ENV VERSION_NODE_DEFAULT=$VERSION_NODE_14
  • Node16をデフォルトにするなら、こんな感じかな、先の nvm use $VERSION_NODE_16 でも良い
ENV VERSION_NODE_DEFAULT=$VERSION_NODE_16

Node16のltsのビルド設定

version: 1
frontend:
  phases:
    preBuild:
      commands:
        - nvm install --lts
        - nvm use $VERSION_NODE_16
        - node -v
        - npm ci
    build:
      commands:
        - npm run build
  • ビルドログ
                                 # Starting phase: preBuild
                                 # Executing command: nvm install --lts
-29T09:18:47.710Z [INFO]: found 0 vulnerabilities
2022-03-29T09:18:47.715Z [INFO]: # Executing command: nvm use 16
2022-03-29T09:18:48.058Z [INFO]: Now using node v16.14.2 (npm v8.5.0)
2022-03-29T09:18:48.059Z [INFO]: # Executing command: node -v
2022-03-29T09:18:48.062Z [INFO]: v16.14.2

16.14.2が使われている

4
1
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
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?