`gatsby build` works locally, but not on netlify CMS

716 Views Asked by At

i'm deploying a prototype to netlify cms to experiment with the blog/markdown functionality that netlify cms offers. however, my build has failed (despite gatsby build working locally). i've seen that exit code 127 is quite a common error, but i've cleared both my gatsby/netlify cache, tried switching to npm run build to get a more comprehensive list of errors (didn't work, just kept printing > [email protected] build > npm run build in my terminal). i'm not sure if this might be due to dependency issues as there were plugins that i had to install with --legacy-peer-deps.

Here's my deploy log and a link to my repo: https://github.com/claudiahleung/gatsby-learnings. Would appreciate any help!

12:02:11 PM: Build ready to start
12:08:45 PM: build-image version: 7b53c5fc4445e3ec99d3949e5d1174a8c5be4f16
12:08:45 PM: build-image tag: v3.7.2
12:08:45 PM: buildbot version: a8df6d1263bb5bcc16f7b4ebb8e9a6cb904cfa8a
12:08:45 PM: Fetching cached dependencies
12:08:45 PM: Failed to fetch cache, continuing with build
12:08:45 PM: Starting to prepare the repo for build
12:08:46 PM: No cached dependencies found. Cloning fresh repo
12:08:46 PM: git clone https://github.com/claudiahleung/gatsby-learnings
12:09:02 PM: Preparing Git Reference refs/heads/main
12:09:04 PM: Parsing package.json dependencies
12:09:05 PM: Starting build script
12:09:05 PM: Installing dependencies
12:09:05 PM: Python version set to 2.7
12:09:06 PM: v12.18.0 is already installed.
12:09:07 PM: Now using node v12.18.0 (npm v6.14.4)
12:09:07 PM: Started restoring cached build plugins
12:09:07 PM: Finished restoring cached build plugins
12:09:07 PM: Attempting ruby version 2.7.1, read from environment
12:09:08 PM: Using ruby version 2.7.1
12:09:08 PM: Using PHP version 5.6
12:09:08 PM: Started restoring cached go cache
12:09:08 PM: Finished restoring cached go cache
12:09:09 PM: go version go1.14.4 linux/amd64
12:09:09 PM: go version go1.14.4 linux/amd64
12:09:09 PM: Installing missing commands
12:09:09 PM: Verify run directory
12:09:10 PM: ​
12:09:10 PM: ────────────────────────────────────────────────────────────────
12:09:10 PM:   Netlify Build                                                 
12:09:10 PM: ────────────────────────────────────────────────────────────────
12:09:10 PM: ​
12:09:10 PM: ❯ Version
12:09:10 PM:   @netlify/build 11.3.2
12:09:10 PM: ​
12:09:10 PM: ❯ Flags
12:09:10 PM:   deployId: 608a2fc31e2066000802ecfa
12:09:10 PM: ​
12:09:10 PM: ❯ Current directory
12:09:10 PM:   /opt/build/repo
12:09:10 PM: ​
12:09:10 PM: ❯ Config file
12:09:10 PM:   No config file was defined: using default values.
12:09:10 PM: ​
12:09:10 PM: ❯ Context
12:09:10 PM:   production
12:09:10 PM: ​
12:09:10 PM: ────────────────────────────────────────────────────────────────
12:09:10 PM:   1. Build command from Netlify app                             
12:09:10 PM: ────────────────────────────────────────────────────────────────
12:09:10 PM: ​
12:09:10 PM: $ gatsby build
12:09:10 PM: bash: gatsby: command not found
12:09:10 PM: ​
12:09:10 PM: ────────────────────────────────────────────────────────────────
12:09:10 PM:   "build.command" failed                                        
12:09:10 PM: ────────────────────────────────────────────────────────────────
12:09:10 PM: ​
12:09:10 PM:   Error message
12:09:10 PM:   Command failed with exit code 127: gatsby build
12:09:10 PM: ​
12:09:10 PM:   Error location
12:09:10 PM:   In Build command from Netlify app:
12:09:10 PM:   gatsby build
12:09:10 PM: ​
12:09:10 PM:   Resolved config
12:09:10 PM:   build:
12:09:10 PM:     command: gatsby build
12:09:10 PM:     commandOrigin: ui
12:09:10 PM:     publish: /opt/build/repo/public
12:09:11 PM: Caching artifacts
12:09:11 PM: Started saving build plugins
12:09:11 PM: Finished saving build plugins
12:09:11 PM: Started saving pip cache
12:09:11 PM: Finished saving pip cache
12:09:11 PM: Started saving emacs cask dependencies
12:09:11 PM: Finished saving emacs cask dependencies
12:09:11 PM: Started saving maven dependencies
12:09:11 PM: Finished saving maven dependencies
12:09:11 PM: Started saving boot dependencies
12:09:11 PM: Finished saving boot dependencies
12:09:11 PM: Started saving rust rustup cache
12:09:11 PM: Finished saving rust rustup cache
12:09:11 PM: Started saving go dependencies
12:09:11 PM: Finished saving go dependencies
12:09:13 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
12:09:13 PM: Creating deploy upload records
12:09:13 PM: Failing build: Failed to build site
12:09:13 PM: Failed during stage 'building site': Build script returned non-zero exit code: 2
12:09:13 PM: Finished processing build request in 28.232494224s
1

There are 1 best solutions below

1
On BEST ANSWER

This issue is generally related to the mismatching of dependencies between your local environment and your Netlify environment causing this output.

Try changing your package.json (and if so, in your Netlify's dashboard) command to:

"build": "npm run build",

If you see locally a lot of warnings about peer-dependencies, then you will need to install them, as the migration guide shows.

Another workaround that might be helpful for you, is to fix your Node versions using the .nvmrc file. Run:

node -v > .nvmrc

This will create a .nvmrc file in the root of your project with the Node version (node -v) in it. Netlify when it's building your site will use that file to set the Node environment as the docs point out. It's very useful when trying to debug the origin of some issues.

In addition, don't upload (remove it as soon as possible and ignore it in the .gitignore) the .cache folder from your repo. You don't want to keep it and it will save you time when pulling/pushing things from your repo (and potentially avoiding conflicts if someone else works in your project).