【Next.js x Netlify】error Command failed with exit code 137. (https://ntl.fyi/exit-code-137)
タイトルに記載のエラーを解決する方法を示します。
概要
本ブログはNext.jsで開発しており、記事の内容はmicroCMSから引いています。記事数が増えてきたところ、後述のエラーでNetlify上でbuildに失敗するようになり、一応解決できたので方法を書いておきます。
エラー内容
build失敗時のログが以下です。
9:27:57 AM: Netlify Build
9:27:57 AM: ────────────────────────────────────────────────────────────────
9:27:57 AM:
9:27:57 AM: ❯ Version
9:27:57 AM: @netlify/build 29.31.1
9:27:57 AM:
9:27:57 AM: ❯ Flags
9:27:57 AM: baseRelDir: true
9:27:57 AM: buildId: 659b416e79522500083a29b3
9:27:57 AM: deployId: 659b416e79522500083a29b5
9:27:57 AM:
9:27:57 AM: ❯ Current directory
9:27:57 AM: /opt/build/repo
9:27:57 AM:
9:27:57 AM: ❯ Config file
9:27:57 AM: /opt/build/repo/netlify.toml
9:27:57 AM:
9:27:57 AM: ❯ Context
9:27:57 AM: production
9:27:57 AM:
9:27:57 AM: ❯ Installing plugins
9:27:57 AM: - @netlify/plugin-nextjs@4.41.3
9:27:57 AM: - @netlify/plugin-emails@1.0.3
9:28:09 AM:
9:28:09 AM: ❯ Using Next.js Runtime - v4.41.3
9:28:09 AM:
9:28:09 AM: ❯ Loading plugins
9:28:09 AM: - @netlify/plugin-emails@1.0.3 from Netlify app
9:28:11 AM:
9:28:11 AM: @netlify/plugin-nextjs (onPreBuild event)
9:28:11 AM: ────────────────────────────────────────────────────────────────
9:28:11 AM:
9:28:11 AM: No Next.js cache to restore.
9:28:11 AM: Netlify configuration property "build.environment.NEXT_PRIVATE_TARGET" value changed.
9:28:11 AM:
9:28:11 AM: (@netlify/plugin-nextjs onPreBuild completed in 54ms)
9:28:11 AM:
9:28:11 AM: Build command from Netlify app
9:28:11 AM: ────────────────────────────────────────────────────────────────
9:28:11 AM:
9:28:11 AM: $ yarn build
9:28:11 AM: yarn run v1.22.19
9:28:11 AM: $ next build
9:28:11 AM: - info Loaded env from /opt/build/repo/.env
9:28:11 AM: - warn No build cache found. Please configure build caching for faster rebuilds. Read more: https://nextjs.org/docs/messages/no-cache
9:28:11 AM: - info Creating an optimized production build...
9:28:15 AM: Browserslist: caniuse-lite is outdated. Please run:
9:28:15 AM: npx update-browserslist-db@latest
9:28:15 AM: Why you should do it regularly: https://github.com/browserslist/update-db#readme
9:28:28 AM: <w> [webpack.cache.PackFileCacheStrategy] Serializing big strings (830kiB) impacts deserialization performance (consider using Buffer instead and decode when needed)
9:28:34 AM: Browserslist: caniuse-lite is outdated. Please run:
9:28:34 AM: npx browserslist@latest --update-db
9:28:34 AM: Why you should do it regularly: https://github.com/browserslist/browserslist#browsers-data-updating
9:28:35 AM: - info Compiled successfully
9:28:35 AM: - info Linting and checking validity of types...
9:28:36 AM: ./src/components/molecules/ProfileCard/ProfileCard.tsx
9:28:36 AM: 35:9 Warning: Image elements must have an alt prop, either with meaningful text, or an empty string for decorative images. jsx-a11y/alt-text
9:28:36 AM: ./src/locales/en.ts
9:28:36 AM: 1:1 Warning: Assign object to a variable before exporting as module default import/no-anonymous-default-export
9:28:36 AM: ./src/locales/ja.ts
9:28:36 AM: 1:1 Warning: Assign object to a variable before exporting as module default import/no-anonymous-default-export
9:28:36 AM: info - Need to disable some ESLint rules? Learn more here: https://nextjs.org/docs/basic-features/eslint#disabling-rules
9:28:39 AM: - info Collecting page data...
9:28:52 AM: - info Generating static pages (0/116)
9:29:01 AM: - info Generating static pages (29/116)
9:29:04 AM: - info Generating static pages (58/116)
9:29:15 AM: Killed
9:29:15 AM: error Command failed with exit code 137. (https://ntl.fyi/exit-code-137)
9:29:15 AM: info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
9:29:15 AM:
9:29:15 AM: "build.command" failed
9:29:15 AM: ────────────────────────────────────────────────────────────────
9:29:15 AM:
9:29:15 AM: Error message
9:29:15 AM: Command failed with exit code 137: yarn build (https://ntl.fyi/exit-code-137)
9:29:15 AM:
9:29:15 AM: Error location
9:29:15 AM: In Build command from Netlify app:
9:29:15 AM: yarn build
9:29:15 AM:
9:29:15 AM: Resolved config
9:29:15 AM: build:
9:29:15 AM: command: yarn build
9:29:15 AM: commandOrigin: ui
9:29:15 AM: environment:
9:29:15 AM: - BASE_URL
9:29:15 AM: - MICROCMS_API_KEY
9:29:15 AM: - MICROCMS_SERVICE_DOMAIN
9:29:15 AM: - NETLIFY_EMAILS_DIRECTORY
9:29:15 AM: - NETLIFY_EMAILS_SECRET
9:29:15 AM: - NEXT_PUBLIC_GA_ID
9:29:15 AM: - NEXT_PUBLIC_GOOGLE_ADSENSE
9:29:15 AM: - NODEMAILER_EMAIL
9:29:15 AM: - NODEMAILER_PASSWORD
9:29:15 AM: - NODE_OPTIONS
9:29:15 AM: - NEXT_PRIVATE_TARGET
9:29:15 AM: publish: /opt/build/repo/.next
9:29:15 AM: publishOrigin: ui
9:29:15 AM: plugins:
9:29:15 AM: - inputs: {}
9:29:15 AM: origin: ui
9:29:15 AM: package: "@netlify/plugin-nextjs"
9:29:15 AM: - inputs: {}
9:29:15 AM: origin: ui
9:29:15 AM: package: "@netlify/plugin-emails"
9:29:17 AM: Failed during stage "building site": Build script returned non-zero exit code: 2
9:29:17 AM: Build failed due to a user error: Build script returned non-zero exit code: 2
9:29:17 AM: Failing build: Failed to build site
9:29:17 AM: Finished processing build request in 1m49.884s
重要なのは以下2行です。
9:29:15 AM: Killed
9:29:15 AM: error Command failed with exit code 137. (https://ntl.fyi/exit-code-137)
調べてみると、どうやら メモリ不足が原因のようです。
解決方法
以下のファイルをプロジェクトのルートに置くとbuildが通るようになりました。
[build.environment]
NODE_OPTIONS = "--max_old_space_size=8192"
また、何度か実行するとこれでもbuildが失敗する場合があったので、package.jsonに以下も追加してみると通りました。
...
"scripts": {
...
"build": "rm -rf .next && NODE_OPTIONS=--max_old_space_size=8192 && next build",
...
},
...
将来的にはフリープランの限界が来そうな気もするので、その場合はまた別の手段を考えようと思います。
2024/08/29 追記
その後記事が増えたところ再発生しましたが、next.config.js ファイルに以下を追加すると通りました。
module.exports = {
// Your configuration...
experimental: {
cpus: 6
}
}