Sanity Portfolio Starter build fails without clearing cache

I’m trying to deploy the Sanity Gatsby Starter Portfolio. I keep getting this error: npm ERR! ERESOLVE could not resolve which is something to do with package versions. I have added the following env variables (Node version, node flag for legacy peer deps and npm version.

I’ve been searching around trying to find this solution, but haven’t found something that works for me. So any advice would be great! I tried this suggestion with no success.

One other note: If I clear my cache and rebuild, the site builds fine. Once I trigger a new deploy from there is when the error occurs.

Site name: https://ainc-gatsby-sanity.netlify.app/

Build Settings

Full log

4:35:20 PM: Build ready to start
4:35:29 PM: build-image version: 0e2f4c52031ab562db66aec633308326e3b108d0 (focal)
4:35:29 PM: build-image tag: focal
4:35:29 PM: buildbot version: 667117c26805e29113bede4ca1afe54070a4e3b1
4:35:29 PM: Fetching cached dependencies
4:35:29 PM: Starting to download cache of 578.4MB
4:35:34 PM: Finished downloading cache in 4.170490317s
4:35:34 PM: Starting to extract cache
4:35:36 PM: Finished extracting cache in 2.001185571s
4:35:36 PM: Finished fetching cache in 6.260466737s
4:35:36 PM: Starting to prepare the repo for build
4:35:36 PM: Preparing Git Reference refs/heads/main
4:35:38 PM: Parsing package.json dependencies
4:35:38 PM: ​❯ Initial build environmentbaseRelDir: truebranch: maincontext: productioncwd: /opt/build/repofeatureFlags: []mode: buildbotrepositoryRoot: /opt/build/repositeId: 395eac55-3897-41ec-be29-b7b9c741fbd0​❯ UI build settingsbaseRelDir: truebuild:  base: /  command: npm run build-web  environment:    - NETLIFY_BUILD_DEBUG    - NODE_VERSION    - NPM_FLAGS    - NPM_VERSION    - SANITY_DEPLOY_STUDIO_TOKEN  publish: web/public​❯ Resolved build environmentbranch: mainbuildDir: /opt/build/repocontext: productionenv: []​❯ Resolved configbuild:  base: /opt/build/repo  command: npm run build-web  commandOrigin: ui  environment:    - NETLIFY_BUILD_DEBUG    - NODE_VERSION    - NPM_FLAGS    - NPM_VERSION    - SANITY_DEPLOY_STUDIO_TOKEN  publish: /opt/build/repo/web/public  publishOrigin: ui
4:35:38 PM: Different build dir detected, going to use the one specified in the Netlify configuration file: '' versus '/' in the Netlify UI
4:35:39 PM: Starting build script
4:35:39 PM: Installing dependencies
4:35:39 PM: Python version set to 2.7
4:35:39 PM: Started restoring cached node version
4:35:40 PM: Finished restoring cached node version
4:35:41 PM: v14.18.1 is already installed.
4:35:41 PM: Now using node v14.18.1 (npm v8.13.2)
4:35:41 PM: Started restoring cached build plugins
4:35:41 PM: Finished restoring cached build plugins
4:35:41 PM: Attempting ruby version 2.7.2, read from environment
4:35:42 PM: Using ruby version 2.7.2
4:35:43 PM: Using PHP version 8.0
4:35:43 PM: No npm workspaces detected
4:35:43 PM: Started restoring cached node modules
4:35:43 PM: Finished restoring cached node modules
4:35:43 PM: Started restoring cached go cache
4:35:43 PM: Finished restoring cached go cache
4:35:43 PM: Installing Go version 1.17 (requested 1.17)
4:35:48 PM: unset GOOS;
4:35:48 PM: unset GOARCH;
4:35:48 PM: export GOROOT='/opt/buildhome/.gimme/versions/go1.17.linux.amd64';
4:35:48 PM: export PATH="/opt/buildhome/.gimme/versions/go1.17.linux.amd64/bin:${PATH}";
4:35:48 PM: go version >&2;
4:35:48 PM: export GIMME_ENV="/opt/buildhome/.gimme/env/go1.17.linux.amd64.env"
4:35:48 PM: go version go1.17 linux/amd64
4:35:48 PM: Installing missing commands
4:35:48 PM: Verify run directory
4:35:50 PM: ​
4:35:50 PM: ────────────────────────────────────────────────────────────────
4:35:50 PM:   Netlify Build                                                 
4:35:50 PM: ────────────────────────────────────────────────────────────────
4:35:50 PM: ​
4:35:50 PM: ❯ Version
4:35:50 PM:   @netlify/build 27.4.2
4:35:50 PM: ​
4:35:50 PM: ❯ Flags
4:35:50 PM:   apiHost: api.netlify.com
4:35:50 PM:   baseRelDir: true
4:35:50 PM:   branch: main
4:35:50 PM:   buildId: 62d9b8888c278c16f038fbed
4:35:50 PM:   buildbotServerSocket: /tmp/netlify-buildbot-socket
4:35:50 PM:   cacheDir: /opt/build/cache
4:35:50 PM:   cachedConfigPath: /tmp/netlify_config.json
4:35:50 PM:   context: production
4:35:50 PM:   cwd: /opt/build/repo
4:35:50 PM:   deployId: 62d9b8888c278c16f038fbef
4:35:50 PM:   edgeFunctionsDistDir: /tmp/edge-62d9b8888c278c16f038fbef
4:35:50 PM:   featureFlags:
4:35:50 PM:     - edge_functions_cache_cli
4:35:50 PM:   framework: unknown
4:35:50 PM:   functionsDistDir: /tmp/zisi-62d9b8888c278c16f038fbef
4:35:50 PM:   mode: buildbot
4:35:50 PM:   nodePath: /opt/buildhome/.nvm/versions/node/v14.18.1/bin/node
4:35:50 PM:   repositoryRoot: /opt/build/repo
4:35:50 PM:   saveConfig: true
4:35:50 PM:   sendStatus: true
4:35:50 PM:   siteId: 395eac55-3897-41ec-be29-b7b9c741fbd0
4:35:50 PM:   statsd:
4:35:50 PM:     host: 10.65.155.70
4:35:50 PM:     port: 8125
4:35:50 PM:   testOpts:
4:35:50 PM:     silentLingeringProcesses: ''
4:35:50 PM: ​
4:35:50 PM: ❯ Current directory
4:35:50 PM:   /opt/build/repo
4:35:50 PM: ​
4:35:50 PM: ❯ Config file
4:35:50 PM:   No config file was defined: using default values.
4:35:50 PM: ​
4:35:50 PM: ❯ Resolved config
4:35:50 PM:   build:
4:35:50 PM:     base: /opt/build/repo
4:35:50 PM:     command: npm run build-web
4:35:50 PM:     commandOrigin: ui
4:35:50 PM:     environment:
4:35:50 PM:       - NETLIFY_BUILD_DEBUG
4:35:50 PM:       - NODE_VERSION
4:35:50 PM:       - NPM_FLAGS
4:35:50 PM:       - NPM_VERSION
4:35:50 PM:       - SANITY_DEPLOY_STUDIO_TOKEN
4:35:50 PM:     publish: /opt/build/repo/web/public
4:35:50 PM:     publishOrigin: ui
4:35:50 PM: ​
4:35:50 PM: ❯ Context
4:35:50 PM:   production
4:35:50 PM: ​
4:35:50 PM: ────────────────────────────────────────────────────────────────
4:35:50 PM:   1. Build command from Netlify app                             
4:35:50 PM: ────────────────────────────────────────────────────────────────
4:35:50 PM: ​
4:35:50 PM: $ npm run build-web
4:35:50 PM: > ainc-gatsby-sanity@0.0.0 build-web
4:35:50 PM: > lerna bootstrap && (cd studio && SANITY_AUTH_TOKEN=$SANITY_DEPLOY_STUDIO_TOKEN npm run graphql-deploy) && (cd web && npm run build)
4:35:50 PM: lerna notice cli v4.0.0
4:35:50 PM: lerna info ci enabled
4:35:51 PM: lerna info Bootstrapping 2 packages
4:35:51 PM: lerna info Installing external dependencies
4:35:53 PM: lerna ERR! npm ci exited 1 in 'ainc-gatsby-sanity-studio'
4:35:53 PM: lerna ERR! npm ci stderr:
4:35:53 PM: npm WARN ERESOLVE overriding peer dependency
4:35:53 PM: npm WARN While resolving: @hot-loader/react-dom@16.14.0+4.13.0
4:35:53 PM: npm WARN Found: react@17.0.2
4:35:53 PM: npm WARN node_modules/react
4:35:53 PM: npm WARN   react@"^17.0.2" from the root project
4:35:53 PM: npm WARN   41 more (@reach/auto-id, @reach/utils, @sanity/base, ...)
4:35:53 PM: npm WARN
4:35:53 PM: npm WARN Could not resolve dependency:
4:35:53 PM: npm WARN peer react@"^16.14.0" from @hot-loader/react-dom@16.14.0+4.13.0
4:35:53 PM: npm WARN node_modules/@hot-loader/react-dom
4:35:53 PM: npm WARN   @hot-loader/react-dom@"^16.9.0-4.12.11" from @sanity/server@2.25.0
4:35:53 PM: npm WARN   node_modules/@sanity/server
4:35:53 PM: npm WARN
4:35:53 PM: npm WARN Conflicting peer dependency: react@16.14.0
4:35:54 PM: Creating deploy upload records
4:35:53 PM: npm WARN node_modules/react
4:35:53 PM: npm WARN   peer react@"^16.14.0" from @hot-loader/react-dom@16.14.0+4.13.0
4:35:53 PM: npm WARN   node_modules/@hot-loader/react-dom
4:35:53 PM: npm WARN     @hot-loader/react-dom@"^16.9.0-4.12.11" from @sanity/server@2.25.0
4:35:53 PM: npm WARN     node_modules/@sanity/server
4:35:54 PM: Failed during stage 'building site': Build script returned non-zero exit code: 2 (https://ntl.fyi/exit-code-2)
4:35:53 PM: npm WARN ERESOLVE overriding peer dependency
4:35:53 PM: npm WARN While resolving: @typescript-eslint/parser@1.13.0
4:35:53 PM: npm WARN Found: eslint@8.8.0
4:35:53 PM: npm WARN node_modules/eslint
4:35:53 PM: npm WARN   dev eslint@"^8.3.0" from the root project
4:35:53 PM: npm WARN   9 more (@typescript-eslint/experimental-utils, babel-eslint, ...)
4:35:53 PM: npm WARN
4:35:53 PM: npm WARN Could not resolve dependency:
4:35:53 PM: npm WARN peer eslint@"^5.0.0" from @typescript-eslint/parser@1.13.0
4:35:53 PM: npm WARN node_modules/@typescript-eslint/parser
4:35:53 PM: npm WARN   @typescript-eslint/parser@"^1.10.2" from prettier-eslint@9.0.2
4:35:53 PM: npm WARN   node_modules/prettier-eslint
4:35:53 PM: npm WARN
4:35:53 PM: npm WARN Conflicting peer dependency: eslint@5.16.0
4:35:53 PM: npm WARN node_modules/eslint
4:35:53 PM: npm WARN   peer eslint@"^5.0.0" from @typescript-eslint/parser@1.13.0
4:35:53 PM: npm WARN   node_modules/@typescript-eslint/parser
4:35:53 PM: npm WARN     @typescript-eslint/parser@"^1.10.2" from prettier-eslint@9.0.2
4:35:53 PM: npm WARN     node_modules/prettier-eslint
4:35:53 PM: npm ERR! code ERESOLVE
4:35:53 PM: npm ERR! ERESOLVE could not resolve
4:35:53 PM: npm ERR!
4:35:53 PM: npm ERR! While resolving: eslint-config-standard@16.0.3
4:35:53 PM: npm ERR! Found: eslint@8.8.0
4:35:53 PM: npm ERR! node_modules/eslint
4:35:53 PM: npm ERR!   dev eslint@"^8.3.0" from the root project
4:35:53 PM: npm ERR!   peer eslint@"*" from @typescript-eslint/experimental-utils@1.13.0
4:35:53 PM: npm ERR!   node_modules/@typescript-eslint/experimental-utils
4:35:53 PM: npm ERR!     @typescript-eslint/experimental-utils@"1.13.0" from @typescript-eslint/parser@1.13.0
4:35:53 PM: npm ERR!     node_modules/@typescript-eslint/parser
4:35:53 PM: npm ERR!       @typescript-eslint/parser@"^1.10.2" from prettier-eslint@9.0.2
4:35:53 PM: npm ERR!       node_modules/prettier-eslint
4:35:53 PM: npm ERR!         prettier-eslint@"^9.0.0" from prettier-eslint-cli@5.0.1
4:35:53 PM: npm ERR!         node_modules/prettier-eslint-cli
4:35:53 PM: npm ERR!   8 more (babel-eslint, eslint-plugin-es, eslint-plugin-import, ...)
4:35:53 PM: npm ERR!
4:35:53 PM: npm ERR! Could not resolve dependency:
4:35:53 PM: npm ERR! peer eslint@"^7.12.1" from eslint-config-standard@16.0.3
4:35:53 PM: npm ERR! node_modules/eslint-config-standard
4:35:53 PM: npm ERR!   dev eslint-config-standard@"^16.0.3" from the root project
4:35:53 PM: npm ERR!
4:35:53 PM: npm ERR! Conflicting peer dependency: eslint@7.32.0
4:35:53 PM: npm ERR! node_modules/eslint
4:35:53 PM: npm ERR!   peer eslint@"^7.12.1" from eslint-config-standard@16.0.3
4:35:53 PM: npm ERR!   node_modules/eslint-config-standard
4:35:53 PM: npm ERR!     dev eslint-config-standard@"^16.0.3" from the root project
4:35:53 PM: npm ERR!
4:35:53 PM: npm ERR! Fix the upstream dependency conflict, or retry
4:35:53 PM: npm ERR! this command with --force, or --legacy-peer-deps
4:35:53 PM: npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
4:35:53 PM: npm ERR!
4:35:53 PM: npm ERR! See /opt/buildhome/.npm/eresolve-report.txt for a full report.
4:35:53 PM: npm ERR! A complete log of this run can be found in:
4:35:53 PM: npm ERR!     /opt/buildhome/.npm/_logs/2022-07-21T20_35_51_526Z-debug-0.log
4:35:53 PM: lerna ERR! npm ci exited 1 in 'ainc-gatsby-sanity-studio'
4:35:53 PM: lerna WARN complete Waiting for 1 child process to exit. CTRL-C to exit immediately.
4:35:53 PM: ​
4:35:53 PM: ────────────────────────────────────────────────────────────────
4:35:53 PM:   "build.command" failed                                        
4:35:53 PM: ────────────────────────────────────────────────────────────────
4:35:53 PM: ​
4:35:53 PM:   Error message
4:35:53 PM:   Command failed with exit code 1: npm run build-web (https://ntl.fyi/exit-code-1)
4:35:53 PM: ​
4:35:53 PM:   Error location
4:35:53 PM:   In Build command from Netlify app:
4:35:53 PM:   npm run build-web
4:35:53 PM: ​
4:35:53 PM:   Resolved config
4:35:53 PM:   build:
4:35:53 PM:     base: /opt/build/repo
4:35:53 PM:     command: npm run build-web
4:35:53 PM:     commandOrigin: ui
4:35:53 PM:     environment:
4:35:53 PM:       - NETLIFY_BUILD_DEBUG
4:35:53 PM:       - NODE_VERSION
4:35:53 PM:       - NPM_FLAGS
4:35:53 PM:       - NPM_VERSION
4:35:53 PM:       - SANITY_DEPLOY_STUDIO_TOKEN
4:35:53 PM:     publish: /opt/build/repo/web/public
4:35:53 PM:     publishOrigin: ui
4:35:54 PM: Caching artifacts
4:35:54 PM: Started saving node modules
4:35:54 PM: Finished saving node modules
4:35:54 PM: Started saving build plugins
4:35:54 PM: Finished saving build plugins
4:35:54 PM: Started saving pip cache
4:35:54 PM: Finished saving pip cache
4:35:54 PM: Started saving emacs cask dependencies
4:35:54 PM: Finished saving emacs cask dependencies
4:35:54 PM: Started saving maven dependencies
4:35:54 PM: Finished saving maven dependencies
4:35:54 PM: Started saving boot dependencies
4:35:54 PM: Finished saving boot dependencies
4:35:54 PM: Started saving rust rustup cache
4:35:54 PM: Finished saving rust rustup cache
4:35:54 PM: Started saving go dependencies
4:35:54 PM: Finished saving go dependencies
4:35:54 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
4:35:54 PM: Failing build: Failed to build site
4:35:54 PM: Finished processing build request in 24.980235697s

Updated to include Debug mode build logs

Update, I have migrated to Yarn Workspaces and it seems to be working without clearing the cache. However, it does take a very long time (2+ min).

Any ideas on how to shorten that time or how to fix my previous issue with npm?

Have you checked: