Zubora Code

【Next.js x Netlify】error Command failed with exit code 137. (https://ntl.fyi/exit-code-137)

Show how to resolve the error on the title.

Published: 8 January, 2024
Revised: 29 August, 2024

Overview

This blog is developed using Next.js, and its content is sourced from microCMS. As the number of articles increased, I encountered the following error, which led to build failures on Netlify. I was able to resolve the issue, so I will document the solution below.


Error details

The log during the build failure is as follows:

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



The following two lines are crucial:

9:29:15 AM: Killed
9:29:15 AM: error Command failed with exit code 137. (https://ntl.fyi/exit-code-137)

Upon investigation, it seems that error code 137 is likely due to insufficient memory.



Solution

Placing the following file in the project's root resolved the build issue:

[build.environment]
    NODE_OPTIONS = "--max_old_space_size=8192"

Additionally, there were instances where the build failed even with the previous solution after multiple attempts. Adding the following to the package.json resolved the issue in those cases:

...
"scripts": {
    ...
    "build": "rm -rf .next && NODE_OPTIONS=--max_old_space_size=8192 && next build",
    ...
  },
...

Considering the potential limitations of the free plan in the future, I might explore alternative approaches when that arises.


Update on 2024/08/29

After adding more articles, the issue reoccurred, but it was resolved by adding the following to the next.config.js file.

module.exports = {
  // Your configuration...
  experimental: {
    cpus: 6
  }
}



Related Links

Toshimitsu Kugimoto

Software Engineer

Specializing in backend and web frontend development for payment and media at work, the go-to languages for these tasks include Java and TypeScript. Additionally, currently exploring Flutter app development as a side project.