Build fails on Github push but succeeds with cleared cache retry

10 replies
Last updated: Aug 24, 2020
I’m having an issue with the build process. Every time an automated build kicks off from a Github push, the build fails. However if I restart/retry the deply with the cache cleared it builds / deploys successfully. EVERY SINGLE TIME.
11
:16:05 AM: sanity-nextjs-landing-pages-studio: > sanity-nextjs-landing-pages-studio@1.0.4 build /opt/build/repo/studio11
:16:05 AM: sanity-nextjs-landing-pages-studio: > sanity build11
:16:05 AM: sanity-nextjs-landing-pages-web: > sanity-nextjs-landing-pages-web@1.0.7 build /opt/build/repo/web11
:16:05 AM: sanity-nextjs-landing-pages-web: > next build && next export && node exportSitemap.js11
:16:05 AM: sanity-nextjs-landing-pages-web: sh: 1: next: not found11
:16:05 AM: sanity-nextjs-landing-pages-web: npm ERR! code ELIFECYCLE11
:16:05 AM: sanity-nextjs-landing-pages-web: npm ERR! syscall spawn11
:16:05 AM: sanity-nextjs-landing-pages-web: npm ERR! file sh11
:16:05 AM: sanity-nextjs-landing-pages-web: npm ERR! errno ENOENT11
:16:05 AM: sanity-nextjs-landing-pages-web: npm ERR! sanity-nextjs-landing-pages-web@1.0.7 build:
next build && next export && node exportSitemap.js
11
:16:05 AM: sanity-nextjs-landing-pages-web: npm ERR! spawn ENOENT11
:16:05 AM: sanity-nextjs-landing-pages-web: npm ERR!11
:16:05 AM: sanity-nextjs-landing-pages-web: npm ERR! Failed at the sanity-nextjs-landing-pages-web@1.0.7 build script.11
:16:05 AM: sanity-nextjs-landing-pages-web: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.11
:16:05 AM: sanity-nextjs-landing-pages-web: npm WARN Local package.json exists, but node_modules missing, did you mean to install?11
:16:05 AM: sanity-nextjs-landing-pages-web: npm ERR! A complete log of this run can be found in:11
:16:05 AM: sanity-nextjs-landing-pages-web: npm ERR! /opt/buildhome/.npm/_logs/2020-08-24T04_16_05_419Z-debug.log
AI Update

This isn't really a Sanity issue - it's a Netlify build configuration problem with your monorepo setup. The error sh: 1: next: not found combined with npm WARN Local package.json exists, but node_modules missing indicates that Netlify isn't properly installing dependencies in your web folder when using cached builds.

While you have a Sanity Studio in your project, the build failure is happening because Netlify's cache restoration isn't triggering dependency installation correctly for your Next.js application. This is a common Netlify monorepo caching issue.

You'll want to reach out to Netlify support or check their documentation for monorepo build configuration. Some common solutions involve:

  • Configuring a netlify.toml file with proper base directory settings
  • Setting the NPM_CONFIG_PRODUCTION environment variable
  • Adjusting your build commands to explicitly install dependencies in each workspace

Since this is about Netlify's build process rather than Sanity's functionality, the Netlify support forums or their documentation on monorepo builds would be the best place to get help with this specific issue.

If you were having issues with sanity build or deploying your Sanity Studio specifically, that's where the Sanity community could help - but in this case, your Studio build is actually succeeding; it's the Next.js build that's failing due to Netlify's dependency management.

Show original thread
10 replies
Is next.js CLI included in your devDependencies and do you run install and/or cache node_modules before running.
I’m only saying because it’s clearly saying that it can’t find the command
next
.
oh doesn’t look like it. Here is my package.json
{
  "private": true,
  "name": "sanity-nextjs-landing-pages",
  "version": "1.0.7",
  "scripts": {
    "build": "lerna run build --parallel",
    "build-web": "lerna bootstrap && (cd web && npm run build)",
    "build-studio": "lerna bootstrap && (cd studio && npm run build)",
    "dev": "lerna run dev --parallel",
    "format": "lerna run format",
    "graphql-deploy": "lerna run graphql-deploy",
    "lint": "lerna run lint",
    "postinstall": "lerna bootstrap",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "devDependencies": {
    "@sanity/cli": "^1.149.19",
    "lerna": "^3.9.0"
  },
  "dependencies": {
    "@zeit/next-sass": "^1.0.1",
    "bootstrap": "^4.5.2",
    "node-sass": "^4.14.1",
    "react-bootstrap": "^1.3.0",
    "sass": "^1.26.10"
  }
}

yes that might help. Though I’m surprised it works with a clean cache if that’s the case.
yeah, I thought it would be part of the starter project
I think they rely on the CLI tool being installed globally for your machine don’t they? It’s been a while since I used next.
yeah it is installed on my machine and works fine locally, it’s the deploy to netify that didn’t work
but looks like adding next.js to my package.json solved it. I owe you a beer!
No worries! Have a good one 👍

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?