Build failing for Gatsby Next pages due to missing dependencies
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 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 build
to:
npm install && npm run build
Or if you're using Lerna's bootstrap:
npm install && npx lerna bootstrap && npm run build
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.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.
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.