Build failing for Gatsby Next pages due to missing dependencies

5 replies
Last updated: Jun 3, 2020
Hey πŸ‘‹ I'm having issues with the build failing for both my studio and front end for the gatsby next pages. I'm getting the following error in the Netlify output..
12:14:42 PM: > next build && next export && node exportSitemap.js
12:14:42 PM: lerna
12:14:42 PM: ERR!
12:14:42 PM: npm run build
12:14:42 PM:  stderr:
12:14:42 PM: sh: 1: next: not found
12:14:42 PM: npm ERR! code ELIFECYCLE
12:14:42 PM: npm ERR! syscall spawn
12:14:42 PM: npm ERR! file sh
12:14:42 PM: npm ERR! errno ENOENT
12:14:42 PM: npm ERR! sanity-nextjs-landing-pages-web@1.0.5 build: `next build && next export && node exportSitemap.js`
12:14:42 PM: npm ERR! spawn ENOENT
12:14:42 PM: npm ERR!
12:14:42 PM: npm ERR! Failed at the sanity-nextjs-landing-pages-web@1.0.5 build script.
12:14:42 PM: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
12:14:42 PM: npm WARN Local package.json exists, but node_modules missing, did you mean to install?
12:14:42 PM: npm ERR! A complete log of this run can be found in:
12:14:42 PM: npm ERR!     /opt/buildhome/.npm/_logs/2020-06-01T11_14_42_427Z-debug.log
12:14:42 PM: lerna
12:14:42 PM: ERR!
12:14:42 PM: npm run build
12:14:42 PM:  exited 1 in 'sanity-nextjs-landing-pages-web'
12:14:42 PM: lerna
12:14:42 PM: WARN
12:14:42 PM: complete
12:14:42 PM:  Waiting for 1 child process to exit. CTRL-C to exit immediately.
12:14:42 PM: npm ERR! code ELIFECYCLE
12:14:42 PM: npm ERR! errno 1
12:14:42 PM: npm ERR! sanity-nextjs-landing-pages@1.0.5 build: `lerna run build --parallel`
12:14:42 PM: npm ERR! Exit status 1
12:14:42 PM: npm ERR!
12:14:42 PM: npm ERR! Failed at the sanity-nextjs-landing-pages@1.0.5 build script.
12:14:42 PM: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
12:14:42 PM: npm
12:14:42 PM: ERR!
12:14:42 PM:  A complete log of this run can be found in:
12:14:42 PM: npm
12:14:42 PM: ERR!
12:14:42 PM:      /opt/buildhome/.npm/_logs/2020-06-01T11_14_42_471Z-debug.log
12:14:42 PM:
12:14:42 PM: β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
12:14:42 PM: β”‚   "build.command" failed    β”‚
12:14:42 PM: β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
12:14:42 PM:
12:14:42 PM:   Error message
12:14:42 PM:   Command failed with exit code 1: npm run build
12:14:42 PM:
12:14:42 PM:   Error location
12:14:42 PM:   In Build command from settings:
12:14:42 PM:   npm run build
12:14:42 PM:
12:14:42 PM:   Resolved config
12:14:42 PM:   build:
12:14:42 PM:     base: /opt/build/repo
12:14:42 PM:     command: npm run build
12:14:42 PM:     publish: /opt/build/repo/web/out
12:14:42 PM: Caching artifacts
12:14:42 PM: Started saving node modules
12:14:42 PM: Finished saving node modules
12:14:42 PM: Started saving build plugins
12:14:42 PM: Finished saving build plugins
12:14:42 PM: Started saving pip cache
12:14:43 PM: Finished saving pip cache
12:14:43 PM: Started saving emacs cask dependencies
12:14:43 PM: Finished saving emacs cask dependencies
12:14:43 PM: Started saving maven dependencies
12:14:43 PM: Finished saving maven dependencies
12:14:43 PM: Started saving boot dependencies
12:14:43 PM: Finished saving boot dependencies
12:14:43 PM: Started saving go dependencies
12:14:43 PM: Finished saving go dependencies
12:14:43 PM: Error running command: Build script returned non-zero exit code: 1
12:14:43 PM: Failing build: Failed to build site
12:14:43 PM: Failed during stage 'building site': Build script returned non-zero exit code: 1
12:14:43 PM: Finished processing build request in 17.856230569s
AI Update

The error you're seeing is a classic dependency installation issue in a monorepo setup. The key messages are:

  • sh: 1: next: not found - Next.js CLI isn't available
  • npm WARN Local package.json exists, but node_modules missing, did you mean to install?

This is happening because your Lerna monorepo isn't installing dependencies properly before the build runs. Here's how to fix it:

Solution

Update your Netlify build settings:

  1. Change your build command in Netlify (Settings β†’ Build & Deploy β†’ Build settings) from:

    npm run build

    to:

    npm install && npm run build

    Or if you're using Lerna's bootstrap:

    npm install && npx lerna bootstrap && npm run build
  2. Set the correct base directory if your Next.js app is in a subdirectory. Based on your error, it looks like it's in /web, so set:

    • Base directory: leave blank (root)
    • Publish directory: web/out

Additional Considerations

For monorepo builds with Lerna, you might need a more specific approach:

  • If you're using npm workspaces or Lerna, make sure your root package.json has all workspace packages defined
  • Consider using npx lerna bootstrap --hoist to hoist common dependencies
  • Your build command might need to be: npm ci && npx lerna bootstrap && npm run build

Note about next export: This command is deprecated in newer Next.js versions. If you're updating your Next.js version, you should use output: 'export' in your next.config.js instead and just run next build.

Quick test locally: Before pushing, try running these commands in sequence locally to ensure they work:

rm -rf node_modules
npm install
npx lerna bootstrap
npm run build

The root cause is that Netlify is trying to run your build script before dependencies are installed in your monorepo structure. Adding the install step to your build command should resolve this.

had the same problem last night deleted the node_modules folder and yarn.lock installed again and the deployment went through.
Are you still seeing this issue? Like Carlos said, removing `package-lock.json`/`yarn.lock.json`, as well as the
node_modules
folder (if they exist) before running
npm install
should resolve the issue.
I did try that but will give it another go and fingers crossed!
Third time lucky πŸ™Œ thanks both!
Awesome, glad to hear!

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?