Deploy Failed Next.js + Sanity app

Hi! I was experiencing some problems trying to deploy my site. https://blog-next-sanity.netlify.app/
I’ve a Next.js + Sanity.js blog site, which are working fine in local and as well in vercel.com (https://next-js-blog-with-comments-five-dusky.vercel.app).
I’d like to use Netlify as a main source, but when I deploy from same github, at some point it turn me fail.
I’ve read docs on Netlify and some other community topic, but also without any luck.
Any advise/tip ?

What problems? Kindly elaborate.

Sorry, yes…
log file says…
4:48:07 PM: Build ready to start
4:48:08 PM: build-image version: d84c79427e8f83c1ba17bcdd7b3fe38059376b68
4:48:08 PM: build-image tag: v3.6.1
4:48:08 PM: buildbot version: a0f5a242397f47e25a65cd8b1de9758023166a1b
4:48:08 PM: Building without cache
4:48:08 PM: Starting to prepare the repo for build
4:48:09 PM: No cached dependencies found. Cloning fresh repo
4:48:09 PM: git clone https://github.com/alexuco/next-js-blog-with-comments
4:48:11 PM: Preparing Git Reference refs/heads/master
4:48:13 PM: Different publish path detected, going to use the one specified in the Netlify configuration file: ‘out’ versus ‘out/’ in the Netlify UI
4:48:13 PM: Starting build script
4:48:13 PM: Installing dependencies
4:48:13 PM: Python version set to 2.7
4:48:15 PM: v12.18.0 is already installed.
4:48:15 PM: Now using node v12.18.0 (npm v6.14.4)
4:48:15 PM: Started restoring cached build plugins
4:48:15 PM: Finished restoring cached build plugins
4:48:16 PM: Attempting ruby version 2.7.1, read from environment
4:48:17 PM: Using ruby version 2.7.1
4:48:18 PM: Using PHP version 5.6
4:48:18 PM: Started restoring cached node modules
4:48:18 PM: Finished restoring cached node modules
4:48:18 PM: Started restoring cached yarn cache
4:48:18 PM: Finished restoring cached yarn cache
4:48:18 PM: Installing yarn at version 1.22.4
4:48:18 PM: Installing Yarn!
4:48:18 PM: > Downloading tarball…
4:48:18 PM: [1/2]: https://yarnpkg.com/downloads/1.22.4/yarn-v1.22.4.tar.gz → /tmp/yarn.tar.gz.qVounuFZUD
4:48:18 PM: % Total % Received % Xferd Average Speed Time Time Time Current
4:48:18 PM: Dload Upload Total Spent Left Speed
4:48:18 PM: 0 0 0 0 0 0 0 0 --:–:-- --:–:-- --:–:-- 0
4:48:18 PM: 100 80 100 80 0 0 301 0 --:–:-- --:–:-- --:–:-- 303
4:48:18 PM: 100 93 100 93 0 0 155 0 --:–:-- --:–:-- --:–:-- 155
4:48:19 PM: 0 0 0 0 0 0 0 0 --:–:-- --:–:-- --:–:-- 0
4:48:19 PM: 100 625 100 625 0 0 425 0 0:00:01 0:00:01 --:–:-- 1134
4:48:20 PM: 100 1215k 100 1215k 0 0 653k 0 0:00:01 0:00:01 --:–:-- 653k
4:48:20 PM: [2/2]: https://yarnpkg.com/downloads/1.22.4/yarn-v1.22.4.tar.gz.asc → /tmp/yarn.tar.gz.qVounuFZUD.asc
4:48:20 PM: 100 84 100 84 0 0 1251 0 --:–:-- --:–:-- --:–:-- 1251
4:48:20 PM: 100 84 100 84 0 0 1249 0 --:–:-- --:–:-- --:–:-- 0
4:48:20 PM: 100 97 100 97 0 0 1023 0 --:–:-- --:–:-- --:–:-- 1023
4:48:20 PM: 100 629 100 629 0 0 1675 0 --:–:-- --:–:-- --:–:-- 1675
4:48:20 PM: 100 1028 100 1028 0 0 2452 0 --:–:-- --:–:-- --:–:-- 2452
4:48:20 PM: > Verifying integrity…
4:48:20 PM: gpg: Signature made Mon 09 Mar 2020 03:52:13 PM UTC using RSA key ID 69475BAA
4:48:20 PM: gpg: Good signature from “Yarn Packaging yarn@dan.cx
4:48:20 PM: gpg: Note: This key has expired!
4:48:20 PM: Primary key fingerprint: 72EC F46A 56B4 AD39 C907 BBB7 1646 B01B 86E5 0310
4:48:20 PM: Subkey fingerprint: 6D98 490C 6F1A CDDD 448E 4595 4F77 6793 6947 5BAA
4:48:20 PM: > GPG signature looks good
4:48:20 PM: > Extracting to ~/.yarn…
4:48:20 PM: > Adding to $PATH…
4:48:21 PM: > Successfully installed Yarn 1.22.4! Please open another terminal where the yarn command will now be available.
4:48:21 PM: Installing NPM modules using Yarn version 1.22.4
4:48:22 PM: yarn install v1.22.4
4:48:22 PM: warning package-lock.json found. Your project contains lock files generated by tools other than Yarn. It is advised not to mix package managers in order to avoid resolution inconsistencies caused by unsynchronized lock files. To clear this warning, remove package-lock.json.
4:48:22 PM: [1/4] Resolving packages…
4:48:23 PM: [2/4] Fetching packages…
4:48:42 PM: info fsevents@2.3.1: The platform “linux” is incompatible with this module.
4:48:42 PM: info “fsevents@2.3.1” is an optional dependency and failed compatibility check. Excluding it from installation.
4:48:42 PM: [3/4] Linking dependencies…
4:48:42 PM: warning " > swr@0.2.3" has incorrect peer dependency “react@^16.11.0”.
4:48:42 PM: warning “lerna > @lerna/version > @lerna/github-client > @octokit/rest > @octokit/plugin-request-log@1.0.3” has unmet peer dependency “@octokit/core@>=3”.
4:48:42 PM: warning " > tailwindcss@2.0.2" has unmet peer dependency “autoprefixer@^10.0.2”.
4:48:42 PM: warning " > tailwindcss@2.0.2" has unmet peer dependency “postcss@^8.0.9”.
4:48:42 PM: warning “tailwindcss > postcss-nested@5.0.3” has unmet peer dependency “postcss@^8.1.13”.
4:48:51 PM: [4/4] Building fresh packages…
4:48:55 PM: $ lerna bootstrap
4:48:56 PM: lerna notice cli v3.22.1
4:48:56 PM: lerna info ci enabled
4:48:56 PM: lerna info Bootstrapping 1 package
4:48:56 PM: lerna info Installing external dependencies
4:49:26 PM: lerna info Symlinking packages and binaries
4:49:26 PM: lerna success Bootstrapped 1 package
4:49:26 PM: Done in 63.95s.
4:49:26 PM: NPM modules installed using Yarn
4:49:26 PM: Started restoring cached go cache
4:49:26 PM: Finished restoring cached go cache
4:49:27 PM: go version go1.14.4 linux/amd64
4:49:27 PM: go version go1.14.4 linux/amd64
4:49:27 PM: Installing missing commands
4:49:27 PM: Verify run directory
4:49:29 PM: ​
4:49:29 PM: ────────────────────────────────────────────────────────────────
4:49:29 PM: Netlify Build
4:49:29 PM: ────────────────────────────────────────────────────────────────
4:49:29 PM: ​
4:49:29 PM: ❯ Version
4:49:29 PM: @netlify/build 8.3.4
4:49:29 PM: ​
4:49:29 PM: ❯ Flags
4:49:29 PM: deployId: 601ac5b666dd4d0116832bd1
4:49:29 PM: mode: buildbot
4:49:29 PM: ​
4:49:29 PM: ❯ Current directory
4:49:29 PM: /opt/build/repo
4:49:29 PM: ​
4:49:29 PM: ❯ Config file
4:49:29 PM: No config file was defined: using default values.
4:49:29 PM: ​
4:49:29 PM: ❯ Context
4:49:29 PM: production
4:49:29 PM: ​
4:49:29 PM: ────────────────────────────────────────────────────────────────
4:49:29 PM: 1. Build command from Netlify app
4:49:29 PM: ────────────────────────────────────────────────────────────────
4:49:29 PM: ​
4:49:29 PM: $ yarn build
4:49:29 PM: yarn run v1.22.4
4:49:29 PM: $ npm run build:sanity && pwd && npm run build:web
4:49:30 PM: npm WARN lifecycle The node binary used for scripts is /tmp/yarn–1612367369654-0.660180213180354/node but npm is using /opt/buildhome/.nvm/versions/node/v12.18.0/bin/node itself. Use the --scripts-prepend-node-path option to include the path for the node binary npm was executed with.
4:49:30 PM: > cms-sanity@1.0.0 build:sanity /opt/build/repo
4:49:30 PM: > cd studio && sanity build …/public/studio -y && cd …
4:49:32 PM: Including the following environment variables as part of the JavaScript bundle:
4:49:32 PM: - SANITY_STUDIO_API_DATASET
4:49:32 PM: - SANITY_STUDIO_API_PROJECT_ID
4:50:50 PM: /opt/build/repo
4:50:51 PM: npm WARN lifecycle The node binary used for scripts is /tmp/yarn–1612367369654-0.660180213180354/node but npm is using /opt/buildhome/.nvm/versions/node/v12.18.0/bin/node itself. Use the --scripts-prepend-node-path option to include the path for the node binary npm was executed with.
4:50:51 PM: > cms-sanity@1.0.0 build:web /opt/build/repo
4:50:51 PM: > next build
4:50:52 PM: warn - No build cache found. Please configure build caching for faster rebuilds. Read more: no-cache | Next.js
4:50:52 PM: info - Creating an optimized production build…
4:50:58 PM: warn - Tailwind is not purging unused styles because no template paths have been provided.
4:50:58 PM: warn - If you have manually configured PurgeCSS outside of Tailwind or are deliberately not removing unused styles, set purge: false in your Tailwind config file to silence this warning.
4:50:58 PM: warn - Optimizing for Production - Tailwind CSS
4:51:23 PM: info - Compiled successfully
4:51:23 PM: info - Collecting page data…
4:51:24 PM: info - Generating static pages (0/7)
4:51:25 PM: info - Generating static pages (1/7)
4:51:26 PM: info - Generating static pages (3/7)
4:51:26 PM: info - Generating static pages (5/7)
4:51:27 PM: info - Generating static pages (7/7)
4:51:27 PM: info - Finalizing page optimization…
4:51:27 PM: Page Size First Load JS
4:51:27 PM: ┌ ● / (ISR: 1 Seconds) 1.05 kB 86.2 kB
4:51:27 PM: ├ /_app 0 B 63.5 kB
4:51:27 PM: ├ ○ /404 2.77 kB 66.3 kB
4:51:27 PM: ├ λ /api/createComment 0 B 63.5 kB
4:51:27 PM: ├ λ /api/exit-preview 0 B 63.5 kB
4:51:27 PM: ├ λ /api/preview 0 B 63.5 kB
4:51:27 PM: └ ● /posts/[slug] 9.84 kB 95 kB
4:51:27 PM: └ css/b3bd4c1805cf7169b7f9.css 159 B
4:51:27 PM: ├ /posts/my-brand-new-blog-powered-by-sanity-io
4:51:27 PM: ├ /posts/picture-perfect-cropping-with-hotspots
4:51:27 PM: ├ /posts/exploration-powered-by-structured-content
4:51:27 PM: └ /posts/my-first-introduction-to-portable-text
4:51:27 PM: + First Load JS shared by all 63.5 kB
4:51:27 PM: ├ chunks/commons.abad50.js 13.3 kB
4:51:27 PM: ├ chunks/framework.29f9e2.js 42.1 kB
4:51:27 PM: ├ chunks/main.231eae.js 6.32 kB
4:51:27 PM: ├ chunks/pages/_app.3eca2b.js 560 B
4:51:27 PM: ├ chunks/webpack.d42ae1.js 1.19 kB
4:51:27 PM: └ css/065ab61783d6c6e41c66.css 2.75 kB
4:51:27 PM: λ (Server) server-side renders at runtime (uses getInitialProps or getServerSideProps)
4:51:27 PM: ○ (Static) automatically rendered as static HTML (uses no initial props)
4:51:27 PM: ● (SSG) automatically generated as static HTML + JSON (uses getStaticProps)
4:51:27 PM: (ISR) incremental static regeneration (uses revalidate in getStaticProps)
4:51:27 PM: Done in 117.73s.
4:51:27 PM: ​
4:51:27 PM: (build.command completed in 1m 58.1s)
4:51:27 PM: ​
4:51:27 PM: ────────────────────────────────────────────────────────────────
4:51:27 PM: 2. Deploy site
4:51:27 PM: ────────────────────────────────────────────────────────────────
4:51:27 PM: ​
4:51:27 PM: ​
4:51:27 PM: ────────────────────────────────────────────────────────────────
4:51:27 PM: Configuration error
4:51:27 PM: ────────────────────────────────────────────────────────────────
4:51:27 PM: ​
4:51:27 PM: Error message
4:51:27 PM: Deploy did not succeed: Deploy directory ‘out’ does not exist
4:51:27 PM: ​
4:51:27 PM: Resolved config
4:51:27 PM: build:
4:51:27 PM: command: yarn build
4:51:27 PM: commandOrigin: ui
4:51:27 PM: environment:
4:51:27 PM: - NEXT_PUBLIC_SANITY_DATASET
4:51:27 PM: - NEXT_PUBLIC_SANITY_PROJECT_ID
4:51:27 PM: - SANITY_API_TOKEN
4:51:27 PM: - SANITY_STUDIO_API_DATASET
4:51:27 PM: - SANITY_STUDIO_API_PROJECT_ID
4:51:27 PM: publish: /opt/build/repo/out
4:51:27 PM: Caching artifacts
4:51:27 PM: Started saving node modules
4:51:27 PM: Finished saving node modules
4:51:27 PM: Started saving build plugins
4:51:27 PM: Finished saving build plugins
4:51:27 PM: Started saving yarn cache
4:51:27 PM: Finished saving yarn cache
4:51:27 PM: Started saving pip cache
4:51:27 PM: Finished saving pip cache
4:51:27 PM: Started saving emacs cask dependencies
4:51:27 PM: Finished saving emacs cask dependencies
4:51:27 PM: Started saving maven dependencies
4:51:27 PM: Finished saving maven dependencies
4:51:27 PM: Started saving boot dependencies
4:51:27 PM: Finished saving boot dependencies
4:51:27 PM: Started saving rust rustup cache
4:51:27 PM: Finished saving rust rustup cache
4:51:27 PM: Started saving go dependencies
4:51:27 PM: Finished saving go dependencies
4:51:30 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
4:51:30 PM: Failing build: Failed to build site
4:51:30 PM: Failed during stage ‘building site’: Build script returned non-zero exit code: 2
4:51:30 PM: Finished processing build request in 3m22.113758251s

Basically, your publish directory doesn’t exist. Try to build locally once and see where exactly the generate output is being saved and the use it as a publish directory.

Thanks @hrishikesh for taking time on this topic.
I did change the output folder to “/” and it seems ok but after few seconds another error came out “Deploy did not succeed: Invalid filename ‘studio/node_modules/es5-ext/reg-exp/#/match/index.js’. Deployed filenames cannot contain # or ? characters”
It continued a bit and finally said :
1:54:50 PM: Finished saving go dependencies

1:54:53 PM: Build failed due to a user error: Build script returned non-zero exit code: 2

1:54:53 PM: Failing build: Failed to build site

1:54:53 PM: Failed during stage ‘building site’: Build script returned non-zero exit code: 2

1:54:53 PM: Finished processing build request in 2m53.337479951s

I’m a bit lost because on local everything was fine and in vercel deployment as well was smooth and easy.
Thanks in advanced.

I don’t think you’ve got the output folder correct. Kindly share the repo if possible.

Sure.
https://github.com/alexuco/next-js-blog-with-comments.git
There it goes…

Your website doesn’t build for me locally:

npm run build

> cms-sanity@1.0.0 build
> npm run build:sanity && cd && npm run build:web


> cms-sanity@1.0.0 build:sanity
> cd studio && sanity build ../public/studio -y && cd ..

√ Clearing output folder (8ms)
√ Building Sanity (33537ms)
√ Building index document (53ms)
√ Minifying JavaScript bundles (28228ms)
C:\Users\Hrishikesh\Desktop\next-js-blog-with-comments-master

> cms-sanity@1.0.0 build:web
> next build

(node:10104) [DEP0148] DeprecationWarning: Use of deprecated folder mapping "./" in the "exports" field module resolution of the package at C:\Users\Hrishikesh\Desktop\next-js-blog-with-comments-master\node_modules\next\node_modules\postcss\package.json.
Update this package.json to use a subpath pattern like "./*".
(Use `node --trace-deprecation ...` to show where the warning was created)
info  - Creating an optimized production build ...(node:10104) [DEP0148] DeprecationWarning: Use of deprecated folder mapping "./" in the "exports" field module resolution of the package at C:\Users\Hrishikesh\Desktop\next-js-blog-with-comments-master\node_modules\tailwindcss\node_modules\postcss\package.json.
Update this package.json to use a subpath pattern like "./*".

warn - Tailwind is not purging unused styles because no template paths have been provided.
warn - If you have manually configured PurgeCSS outside of Tailwind or are deliberately not removing unused styles, set `purge: false` in your Tailwind config file to silence this warning.
warn - https://tailwindcss.com/docs/controlling-file-size/#removing-unused-css
info  - Creating an optimized production build
info  - Compiled successfully
info  - Collecting page data  
[    ] info  - Generating static pages (0/3)
Error occurred prerendering page "/". Read more: https://err.sh/next.js/prerender-error
TypeError: Cannot read property 'filter' of undefined
    at getUniquePosts (C:\Users\Hrishikesh\Desktop\next-js-blog-with-comments-master\.next\server\pages\index.js:3199:16)
    at getAllPostsForHome (C:\Users\Hrishikesh\Desktop\next-js-blog-with-comments-master\.next\server\pages\index.js:3239:10)
    at processTicksAndRejections (node:internal/process/task_queues:93:5)
    at async getStaticProps (C:\Users\Hrishikesh\Desktop\next-js-blog-with-comments-master\.next\server\pages\index.js:1266:20)
    at async renderToHTML (C:\Users\Hrishikesh\Desktop\next-js-blog-with-comments-master\node_modules\next\dist\next-server\server\render.js:28:109)
    at async C:\Users\Hrishikesh\Desktop\next-js-blog-with-comments-master\node_modules\next\dist\export\worker.js:26:6
    at async C:\Users\Hrishikesh\Desktop\next-js-blog-with-comments-master\node_modules\next\dist\build\tracer.js:1:1441
info  - Generating static pages (3/3)

> Build error occurred
Error: Export encountered errors on following paths:
        /
    at C:\Users\Hrishikesh\Desktop\next-js-blog-with-comments-master\node_modules\next\dist\export\index.js:30:1103
    at async C:\Users\Hrishikesh\Desktop\next-js-blog-with-comments-master\node_modules\next\dist\build\tracer.js:1:1441
    at async C:\Users\Hrishikesh\Desktop\next-js-blog-with-comments-master\node_modules\next\dist\build\index.js:39:69
    at async C:\Users\Hrishikesh\Desktop\next-js-blog-with-comments-master\node_modules\next\dist\build\tracer.js:1:1441
    at async C:\Users\Hrishikesh\Desktop\next-js-blog-with-comments-master\node_modules\next\dist\build\index.js:21:731
    at async C:\Users\Hrishikesh\Desktop\next-js-blog-with-comments-master\node_modules\next\dist\build\tracer.js:1:1441
npm ERR! code 1
npm ERR! path C:\Users\Hrishikesh\Desktop\next-js-blog-with-comments-master
npm ERR! command failed
npm ERR! command C:\Windows\system32\cmd.exe /d /s /c next build

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Hrishikesh\AppData\Local\npm-cache\_logs\2021-02-05T11_22_51_936Z-debug.log
npm ERR! code 1
npm ERR! path C:\Users\Hrishikesh\Desktop\next-js-blog-with-comments-master
npm ERR! command failed
npm ERR! command C:\Windows\system32\cmd.exe /d /s /c npm run build:sanity && cd && npm run build:web

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Hrishikesh\AppData\Local\npm-cache\_logs\2021-02-05T11_22_51_996Z-debug.log

EDIT: Just to make it clear, the Sanity part builds successfully:

However, it’s the NextJS build that’s not working. If it’s building fine for you, just check in which folder you’re getting your website’s files.

1 Like