Unable to Migrate from Gatsby Cloud: build.command Failed with Exit Code 137 on Netlify

I recently moved my project to Netlify as Gatsby Cloud is shutting down soon. During the build process on Netlify, I encounter the error "build.command" failed Command failed with exit code 137: CI= npm run build . The project runs without issues locally and was previously deploying correctly on Gatsby Cloud. Here are the relevant sections from my package.json, netlify.toml, environment variables, and build log:

package.json

 "scripts": {
    "build": "gatsby build",
    "develop": "gatsby develop",
    "format": "prettier --write \"**/*.{js,jsx,ts,tsx,json,md}\"",
    "start": "npm run develop",
    "serve": "gatsby serve",
    "clean": "gatsby clean",
    "test": "echo \"Write tests! -> https://gatsby.dev/unit-testing\" && exit 1"
  },

netlify.toml

[build]
    command = "CI= npm run build"

[[plugins]]
    package = "@netlify/plugin-gatsby"

Environment Variables

CI false
NODE_OPTIONS --max-old-space-size=4096
NPM_FLAGS --legacy-peer-deps
WPGRAPHQL_URL ***

Build Log

4:23:03 PM: Netlify Build                                                 
4:23:03 PM: ────────────────────────────────────────────────────────────────
4:23:03 PM: ​
4:23:03 PM: ❯ Version
4:23:03 PM:   @netlify/build 29.21.1
4:23:03 PM: ​
4:23:03 PM: ❯ Flags
4:23:03 PM:   baseRelDir: true
4:23:03 PM:   buildId: ***
4:23:03 PM:   deployId: ***
4:23:03 PM: ​
4:23:03 PM: ❯ Current directory
4:23:03 PM:   /opt/build/repo
4:23:03 PM: ​
4:23:03 PM: ❯ Config file
4:23:03 PM:   /opt/build/repo/netlify.toml
4:23:03 PM: ​
4:23:03 PM: ❯ Context
4:23:03 PM:   production
4:23:03 PM: ​
4:23:03 PM: ❯ Installing plugins
4:23:03 PM:    - @netlify/plugin-gatsby@3.7.0
4:23:16 PM: ​
4:23:16 PM: ❯ Loading plugins
4:23:16 PM:    - @netlify/plugin-gatsby@3.7.0 from netlify.toml
4:23:17 PM: ​
4:23:17 PM: @netlify/plugin-gatsby (onPreBuild event)                     
4:23:17 PM: ────────────────────────────────────────────────────────────────
4:23:17 PM: ​
4:23:18 PM: Skipping @netlify/plugin-gatsby work, because used Gatsby version supports adapters.
4:23:18 PM: ​
4:23:18 PM: (@netlify/plugin-gatsby onPreBuild completed in 65ms)
4:23:18 PM: ​
4:23:18 PM: build.command from netlify.toml                               
4:23:18 PM: ────────────────────────────────────────────────────────────────
4:23:18 PM: ​
4:23:18 PM: $ CI= npm run build
4:23:18 PM: > gatsby-starter-wordpress-blog@1.0.0 build
4:23:18 PM: > gatsby build
4:23:20 PM: success compile gatsby files - 0.894s
4:23:20 PM: success load gatsby config - 0.048s
4:23:20 PM: info Using gatsby-adapter-netlify adapter
4:23:21 PM: success load plugins - 0.809s
4:23:21 PM: success onPreInit - 0.003s
4:23:21 PM: success initialize cache - 0.046s
4:23:21 PM: success copy gatsby files - 0.104s
4:23:21 PM: success Compiling Gatsby Functions - 0.139s
4:23:21 PM: success onPreBootstrap - 0.151s
4:23:27 PM: success  gatsby-source-wordpress  ensuring plugin requirements are met - 4.886s
4:23:28 PM: ⠀
4:23:28 PM: info  gatsby-source-wordpress 
4:23:28 PM: 	This is either your first build or the cache was cleared.
4:23:28 PM: 	Please wait while your WordPress data is synced to your Gatsby cache.
4:23:28 PM: 	Maybe now"s a good time to get up and stretch? :D
4:23:31 PM: success  gatsby-source-wordpress  ingest WPGraphQL schema - 4.395s
4:23:31 PM: success createSchemaCustomization - 9.304s
4:23:33 PM: success  gatsby-source-wordpress  fetch root fields - 1.391s
4:23:34 PM: success  gatsby-source-wordpress  Comment - 2.290s - fetched 0
4:23:35 PM: success  gatsby-source-wordpress  UserRole - 3.564s - fetched 0
4:23:35 PM: success  gatsby-source-wordpress  Menu - 3.786s - fetched 1
4:23:35 PM: success  gatsby-source-wordpress  Taxonomy - 3.859s - fetched 3
4:23:35 PM: success  gatsby-source-wordpress  PostFormat - 3.926s - fetched 2
4:23:35 PM: success  gatsby-source-wordpress  MenuItem - 3.985s - fetched 12
4:23:35 PM: success  gatsby-source-wordpress  User - 4.089s - fetched 2
4:23:35 PM: success  gatsby-source-wordpress  Tag - 4.096s - fetched 8
4:23:35 PM: success  gatsby-source-wordpress  Schedule - 4.098s - fetched 8
4:23:36 PM: success  gatsby-source-wordpress  Page - 4.802s - fetched 15
4:23:36 PM: success  gatsby-source-wordpress  ContentType - 5.093s - fetched 4
4:23:37 PM: success  gatsby-source-wordpress  Category - 5.859s - fetched 11
4:23:43 PM: success  gatsby-source-wordpress  Post - 12.125s - fetched 200
4:31:38 PM: success  gatsby-source-wordpress  MediaItem - 474.723s - fetched 1776
4:31:38 PM: success  gatsby-source-wordpress  creating nodes - 474.725s
4:31:38 PM: success  gatsby-source-wordpress  fetching nodes - 486.856s - 2042 total
4:31:38 PM: success Downloading remote files - 473.312s - 1770/1770 3.74/s
4:31:38 PM: success Checking for changed pages - 0.000s
4:31:38 PM: success source and transform nodes - 487.175s
4:31:39 PM: info Writing GraphQL type definitions to /opt/build/repo/.cache/schema.gql
4:31:39 PM: success building schema - 0.628s
4:31:39 PM: success createPages - 0.212s
4:31:39 PM: success createPagesStatefully - 0.067s
4:31:39 PM: info Total nodes: 5889, SitePage nodes: 261 (use --verbose for breakdown)
4:31:39 PM: success Checking for changed pages - 0.000s
4:31:39 PM: success onPreExtractQueries - 0.001s
4:31:41 PM: success  gatsby-source-wordpress  diff schemas - 1.135s
4:31:41 PM: success  gatsby-source-wordpress  ingest WPGraphQL schema - 1.135s
4:31:48 PM: success extract queries from components - 9.003s
4:31:48 PM: success write out redirect data - 0.355s
4:31:50 PM: success Build manifest and related icons - 1.429s
4:31:50 PM: success onPostBootstrap - 1.439s
4:31:50 PM: info bootstrap finished - 512.102s
4:31:50 PM: success write out requires - 0.205s
4:31:52 PM: warning `isModuleDeclaration` has been deprecated, please migrate to `isImportOrExportDeclaration`
4:31:52 PM:     at isModuleDeclaration (/opt/build/repo/node_modules/@babel/types/lib/validators/generated/index.js:2740:35)
4:31:52 PM:     at PluginPass.Program (/opt/build/repo/node_modules/babel-plugin-lodash/lib/index.js:102:44)
4:32:02 PM: success Building production JavaScript and CSS bundles - 12.373s
4:32:11 PM: success Building HTML renderer - 8.561s
4:32:11 PM: success Execute page configs - 0.040s
4:32:11 PM: success Caching Webpack compilations - 0.000s
4:32:24 PM: success run queries in workers - 13.343s - 267/267 20.01/s
4:35:37 PM: Killed
4:35:37 PM: ​
4:35:37 PM: "build.command" failed                                        
4:35:37 PM: ────────────────────────────────────────────────────────────────
4:35:37 PM: ​
4:35:37 PM:   Error message
4:35:37 PM:   Command failed with exit code 137: CI= npm run build (https://ntl.fyi/exit-code-137)
4:35:37 PM: ​
4:35:37 PM:   Error location
4:35:37 PM:   In build.command from netlify.toml:
4:35:37 PM:   CI= npm run build
4:35:37 PM: ​
4:35:37 PM:   Resolved config
4:35:37 PM:   build:
4:35:37 PM:     command: CI= npm run build
4:35:37 PM:     commandOrigin: config
4:35:37 PM:     environment:
4:35:37 PM:       - CI
4:35:37 PM:       - NODE_OPTIONS
4:35:37 PM:       - NPM_FLAGS
4:35:37 PM:       - WPGRAPHQL_URL
4:35:37 PM:     publish: /opt/build/repo/public
4:35:37 PM:     publishOrigin: ui
4:35:37 PM:   plugins:
4:35:37 PM:     - inputs: {}
4:35:37 PM:       origin: config
4:35:37 PM:       package: "@netlify/plugin-gatsby"
4:35:43 PM: Failed during stage "building site": Build script returned non-zero exit code: 2
4:35:43 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
4:35:43 PM: Failing build: Failed to build site
4:35:43 PM: Finished processing build request in 13m19.228s

Yur build is running out of memory. On Starter plan you only get 8 GB of memory, on Pro and above, you get 11 GB. Either fit your build within that memory usage, or build locally and deploy using Netlify CLI.

Thank you!
I understand the reason now. I will optimize the project and try again.