Netlify build failing: "next: not found" error in Sanity Next.js project
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.856230569sThe 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 availablenpm 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:
Change your build command in Netlify (Settings → Build & Deploy → Build settings) from:
npm run buildto:
npm install && npm run buildOr if you're using Lerna's bootstrap:
npm install && npx lerna bootstrap && npm run buildSet 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.jsonhas all workspace packages defined - Consider using
npx lerna bootstrap --hoistto 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 buildThe 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.
Show original thread5 replies
Was this answer helpful?
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.