Nextjs Netlify CMS Starter Failing

Hi I am trying to integrated the Next.js CMS starter template here:

The app shows the following message, is anyone else having these issues? Thanks in advance!

10:34:18 PM: ​
10:34:18 PM: ────────────────────────────────────────────────────────────────
10:34:18 PM: Plugin “@netlify/plugin-nextjs” failed
10:34:18 PM: ────────────────────────────────────────────────────────────────
10:34:18 PM: ​
10:34:18 PM: Error message
10:34:18 PM: Error: The directory “/opt/build/repo/dist” does not contain a Next.js production build. Perhaps the build command was not run, or you specified the wrong publish directory.
10:34:18 PM: In most cases it should be set to “.next”, unless you have chosen a custom “distDir” in your Next config.
10:34:18 PM: If you are using “next export” then you should set the environment variable NETLIFY_NEXT_PLUGIN_SKIP to “true”.
10:34:18 PM: ​
10:34:18 PM: Plugin details
10:34:18 PM: Package: @netlify/plugin-nextjs
10:34:18 PM: Version: 4.12.2
10:34:18 PM: Repository: git+https://github.com/netlify/netlify-plugin-nextjs.git
10:34:18 PM: npm link: https://www.npmjs.com/package/@netlify/plugin-nextjs
10:34:18 PM: Report issues: Issues · netlify/netlify-plugin-nextjs · GitHub
10:34:18 PM: ​
10:34:18 PM: Error location
10:34:18 PM: In “onBuild” event in “@netlify/plugin-nextjs” from Netlify app
10:34:18 PM: at checkNextSiteHasBuilt (/.netlify/plugins/node_modules/@netlify/plugin-nextjs/lib/helpers/verification.js:72:16)
10:34:18 PM: at onBuild (/.netlify/plugins/node_modules/@netlify/plugin-nextjs/lib/index.js:42:50)

You’re probably using older Next.js.

Try following this line.

Hi, @hrishikesh,

I came to the forums just to post the same issue as I’m in the same boat as OP.
At the same time, I think the error we are facing is simply the symptom of some bigger underlying problem in the template.

Per the link posted by OP,

Netlify then automatically creates a clone of the repository in your GitHub or GitLab account

This did not happen for me, I only got an empty repo after going through the flow started by htiting Deploy to Netlify on the same site.

What’s even weirder is that looking through the Netlify logs, it seems that the build process cloned the incorrect git repo. Note on line 9, it’s cloning an unrelated user’s repo, it should be korompaiistvan/nextjs-netlify-blog-template · GitHub
Here is the entire log:

11:04:10 AM: Build ready to start
11:04:11 AM: build-image version: d7b3dbfb0846505993c9a131894d1858074c90b4 (focal)
11:04:11 AM: build-image tag: v4.10.1
11:04:11 AM: buildbot version: 7355c76b6726892b891fe16f63c74b913bc92de2
11:04:11 AM: Fetching cached dependencies
11:04:11 AM: Failed to fetch cache, continuing with build
11:04:11 AM: Starting to prepare the repo for build
11:04:12 AM: No cached dependencies found. Cloning fresh repo
11:04:12 AM: git clone https://github.com/wutali/nextjs-netlify-blog-template
11:04:12 AM: Preparing Git Reference refs/heads/master
11:04:13 AM: Parsing package.json dependencies
11:04:14 AM: Starting build script
11:04:14 AM: Installing dependencies
11:04:14 AM: Python version set to 2.7
11:04:14 AM: v16.16.0 is already installed.
11:04:15 AM: Now using node v16.16.0 (npm v8.11.0)
11:04:15 AM: Started restoring cached build plugins
11:04:15 AM: Finished restoring cached build plugins
11:04:15 AM: Attempting ruby version 2.7.2, read from environment
11:04:16 AM: Using ruby version 2.7.2
11:04:16 AM: Using PHP version 8.0
11:04:16 AM: Started restoring cached yarn cache
11:04:16 AM: Finished restoring cached yarn cache
11:04:16 AM: No yarn workspaces detected
11:04:16 AM: Started restoring cached node modules
11:04:16 AM: Finished restoring cached node modules
11:04:16 AM: Installing NPM modules using Yarn version 1.22.10
11:04:17 AM: npm WARN config tmp This setting is no longer used.  npm stores temporary files in a special
11:04:17 AM: npm WARN config location in the cache, and they are managed by
11:04:17 AM: npm WARN config     [`cacache`](http://npm.im/cacache).
11:04:17 AM: yarn install v1.22.10
11:04:17 AM: 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.
11:04:17 AM: [1/4] Resolving packages...
11:04:17 AM: [2/4] Fetching packages...
11:04:27 AM: info fsevents@2.3.2: The platform "linux" is incompatible with this module.
11:04:27 AM: info "fsevents@2.3.2" is an optional dependency and failed compatibility check. Excluding it from installation.
11:04:27 AM: [3/4] Linking dependencies...
11:04:27 AM: warning " > next-mdx-remote@2.1.3" has incorrect peer dependency "react@>=16.13.1 <=17.0.1".
11:04:27 AM: warning " > next-mdx-remote@2.1.3" has incorrect peer dependency "react-dom@>=16.13.1 <=17.0.1".
11:04:27 AM: warning " > react-instagram-embed@2.0.0" has incorrect peer dependency "react@^16.0.0 || ^15.0.0 || ~0.14.0".
11:04:27 AM: warning " > react-instagram-embed@2.0.0" has incorrect peer dependency "react-dom@^16.0.0 || ^15.0.0 || ~0.14.0".
11:04:27 AM: warning " > react-twitter-embed@3.0.3" has unmet peer dependency "prop-types@^15.5.4".
11:04:27 AM: warning " > react-twitter-embed@3.0.3" has incorrect peer dependency "react@^15.0.0 || ^16.0.0".
11:04:27 AM: warning " > react-twitter-embed@3.0.3" has incorrect peer dependency "react-dom@^15.0.0 || ^16.0.0".
11:04:27 AM: warning "react-twitter-embed > enzyme-adapter-react-16@1.15.6" has unmet peer dependency "enzyme@^3.0.0".
11:04:27 AM: warning "react-twitter-embed > enzyme-adapter-react-16@1.15.6" has incorrect peer dependency "react@^16.0.0-0".
11:04:27 AM: warning "react-twitter-embed > enzyme-adapter-react-16@1.15.6" has incorrect peer dependency "react-dom@^16.0.0-0".
11:04:27 AM: warning "react-twitter-embed > enzyme-adapter-react-16 > enzyme-adapter-utils@1.14.0" has incorrect peer dependency "react@0.13.x || 0.14.x || ^15.0.0-0 || ^16.0.0-0".
11:04:27 AM: warning "react-twitter-embed > enzyme-adapter-react-16 > react-test-renderer@16.14.0" has incorrect peer dependency "react@^16.14.0".
11:04:27 AM: warning "react-twitter-embed > enzyme-adapter-react-16 > enzyme-adapter-utils > airbnb-prop-types@2.16.0" has incorrect peer dependency "react@^0.14 || ^15.0.0 || ^16.0.0-alpha".
11:04:31 AM: [4/4] Building fresh packages...
11:04:31 AM: success Saved lockfile.
11:04:31 AM: Done in 14.60s.
11:04:31 AM: NPM modules installed using Yarn
11:04:32 AM: Started restoring cached go cache
11:04:32 AM: Finished restoring cached go cache
11:04:32 AM: Installing Go version 1.17 (requested 1.17)
11:04:37 AM: unset GOOS;
11:04:37 AM: unset GOARCH;
11:04:37 AM: export GOROOT='/opt/buildhome/.gimme/versions/go1.17.linux.amd64';
11:04:37 AM: export PATH="/opt/buildhome/.gimme/versions/go1.17.linux.amd64/bin:${PATH}";
11:04:37 AM: go version >&2;
11:04:37 AM: export GIMME_ENV="/opt/buildhome/.gimme/env/go1.17.linux.amd64.env"
11:04:37 AM: go version go1.17 linux/amd64
11:04:37 AM: Installing missing commands
11:04:37 AM: Verify run directory
11:04:38 AM: ​
11:04:38 AM: ────────────────────────────────────────────────────────────────
11:04:38 AM:   Netlify Build                                                 
11:04:38 AM: ────────────────────────────────────────────────────────────────
11:04:38 AM: ​
11:04:38 AM: ❯ Version
11:04:38 AM:   @netlify/build 27.6.0
11:04:38 AM: ​
11:04:38 AM: ❯ Flags
11:04:38 AM:   baseRelDir: true
11:04:38 AM:   buildId: 62e6458af9d40232d002612b
11:04:38 AM:   deployId: 62e6458af9d40232d002612d
11:04:38 AM: ​
11:04:38 AM: ❯ Current directory
11:04:38 AM:   /opt/build/repo
11:04:38 AM: ​
11:04:38 AM: ❯ Config file
11:04:38 AM:   /opt/build/repo/netlify.toml
11:04:38 AM: ​
11:04:38 AM: ❯ Context
11:04:38 AM:   production
11:04:38 AM: ​
11:04:38 AM: ❯ Installing plugins
11:04:38 AM:    - @netlify/plugin-nextjs@4.12.2
11:04:49 AM: ​
11:04:49 AM: ❯ Loading plugins
11:04:49 AM:    - @netlify/plugin-nextjs@4.12.2 from Netlify app
11:04:50 AM: ​
11:04:50 AM: ────────────────────────────────────────────────────────────────
11:04:50 AM:   1. @netlify/plugin-nextjs (onPreBuild event)                  
11:04:50 AM: ────────────────────────────────────────────────────────────────
11:04:50 AM: ​
11:04:50 AM: No Next.js cache to restore.
11:04:50 AM: Netlify configuration property "build.environment.NEXT_PRIVATE_TARGET" value changed.
11:04:50 AM: ​
11:04:50 AM: (@netlify/plugin-nextjs onPreBuild completed in 22ms)
11:04:50 AM: ​
11:04:50 AM: ────────────────────────────────────────────────────────────────
11:04:50 AM:   2. build.command from netlify.toml                            
11:04:50 AM: ────────────────────────────────────────────────────────────────
11:04:50 AM: ​
11:04:50 AM: $ npm run export
11:04:51 AM: npm WARN config tmp This setting is no longer used.  npm stores temporary files in a special
11:04:51 AM: npm WARN config location in the cache, and they are managed by
11:04:51 AM: npm WARN config     [`cacache`](http://npm.im/cacache).
11:04:51 AM: > my-app@0.1.1 export
11:04:51 AM: > next build && next export -o ./dist
11:04:51 AM: Browserslist: caniuse-lite is outdated. Please run:
11:04:51 AM: npx browserslist@latest --update-db
11:04:51 AM: Why you should do it regularly:
11:04:51 AM: https://github.com/browserslist/browserslist#browsers-data-updating
11:04:51 AM: (node:1613) [DEP0148] DeprecationWarning: Use of deprecated folder mapping "./" in the "exports" field module resolution of the package at /opt/build/repo/node_modules/next/node_modules/postcss/package.json.
11:04:51 AM: Update this package.json to use a subpath pattern like "./*".
11:04:51 AM: (Use `node --trace-deprecation ...` to show where the warning was created)
11:04:52 AM: info  - Using webpack 4. Reason: future.webpack5 option not enabled https://nextjs.org/docs/messages/webpack5
11:04:52 AM: warn  - No build cache found. Please configure build caching for faster rebuilds. Read more: https://nextjs.org/docs/messages/no-cache
11:04:52 AM: info  - Checking validity of types...
11:04:56 AM: info  - Creating an optimized production build...
11:04:56 AM: Browserslist: caniuse-lite is outdated. Please run:
11:04:56 AM: npx browserslist@latest --update-db
11:04:56 AM: Why you should do it regularly:
11:04:56 AM: https://github.com/browserslist/browserslist#browsers-data-updating
11:05:02 AM: Browserslist: caniuse-lite is outdated. Please run:
11:05:02 AM: npx browserslist@latest --update-db
11:05:02 AM: Why you should do it regularly:
11:05:02 AM: https://github.com/browserslist/browserslist#browsers-data-updating
11:05:11 AM: info  - Compiled successfully
11:05:11 AM: info  - Collecting page data...
11:05:12 AM: info  - Generating static pages (0/17)
11:05:14 AM: info  - Generating static pages (4/17)
11:05:14 AM: info  - Generating static pages (8/17)
11:05:16 AM: Browserslist: caniuse-lite is outdated. Please run:
11:05:16 AM: npx browserslist@latest --update-db
11:05:16 AM: Why you should do it regularly:
11:05:16 AM: https://github.com/browserslist/browserslist#browsers-data-updating
11:05:16 AM: Browserslist: caniuse-lite is outdated. Please run:
11:05:16 AM: npx browserslist@latest --update-db
11:05:16 AM: Why you should do it regularly:
11:05:16 AM: https://github.com/browserslist/browserslist#browsers-data-updating
11:05:16 AM: Browserslist: caniuse-lite is outdated. Please run:
11:05:16 AM: npx browserslist@latest --update-db
11:05:16 AM: Why you should do it regularly:
11:05:16 AM: https://github.com/browserslist/browserslist#browsers-data-updating
11:05:16 AM: Browserslist: caniuse-lite is outdated. Please run:
11:05:16 AM: npx browserslist@latest --update-db
11:05:16 AM: Why you should do it regularly:
11:05:16 AM: https://github.com/browserslist/browserslist#browsers-data-updating
11:05:16 AM: Browserslist: caniuse-lite is outdated. Please run:
11:05:16 AM: npx browserslist@latest --update-db
11:05:16 AM: Why you should do it regularly:
11:05:16 AM: https://github.com/browserslist/browserslist#browsers-data-updating
11:05:16 AM: Browserslist: caniuse-lite is outdated. Please run:
11:05:16 AM: npx browserslist@latest --update-db
11:05:16 AM: Why you should do it regularly:
11:05:16 AM: https://github.com/browserslist/browserslist#browsers-data-updating
11:05:17 AM: info  - Generating static pages (12/17)
11:05:17 AM: info  - Generating static pages (17/17)
11:05:17 AM: info  - Finalizing page optimization...
11:05:18 AM: Page                                Size     First Load JS
11:05:18 AM: ┌ ○ /                               2.54 kB        74.6 kB
11:05:18 AM: ├   /_app                           0 B            63.8 kB
11:05:18 AM: ├ ○ /404                            2.77 kB        66.6 kB
11:05:18 AM: ├ ● /posts                          1.35 kB        80.9 kB
11:05:18 AM: ├ ● /posts/[post]                   16.6 kB        96.1 kB
11:05:18 AM: ├   └ css/d9f998f12db559bf9cc0.css  631 B
11:05:18 AM: ├   ├ /posts/welcome
11:05:18 AM: ├   ├ /posts/markdown-syntax
11:05:18 AM: ├   ├ /posts/rich-content-with-mdx
11:05:18 AM: ├   └ [+3 more paths]
11:05:18 AM: ├ ● /posts/page/[page]              1.36 kB        80.9 kB
11:05:18 AM: ├   └ /posts/page/2
11:05:18 AM: └ ● /posts/tags/[[...slug]]         1.32 kB        80.8 kB
11:05:18 AM:     ├ /posts/tags/document
11:05:18 AM:     ├ /posts/tags/mock
11:05:18 AM:     ├ /posts/tags/style
11:05:18 AM:     └ [+3 more paths]
11:05:18 AM: + First Load JS shared by all       63.8 kB
11:05:18 AM:   ├ chunks/commons.5bc016.js        13.6 kB
11:05:18 AM:   ├ chunks/framework.2d0daf.js      42.1 kB
11:05:18 AM:   ├ chunks/main.0de564.js           6.8 kB
11:05:18 AM:   ├ chunks/pages/_app.5168e8.js     581 B
11:05:18 AM:   ├ chunks/webpack.50bee0.js        751 B
11:05:18 AM:   └ css/7e37b93ade22ad15b8af.css    1.01 kB
11:05:18 AM: λ  (Server)  server-side renders at runtime (uses getInitialProps or getServerSideProps)
11:05:18 AM: ○  (Static)  automatically rendered as static HTML (uses no initial props)
11:05:18 AM: ●  (SSG)     automatically generated as static HTML + JSON (uses getStaticProps)
11:05:18 AM:    (ISR)     incremental static regeneration (uses revalidate in getStaticProps)
11:05:18 AM: info  - Using webpack 4. Reason: future.webpack5 option not enabled https://nextjs.org/docs/messages/webpack5
11:05:18 AM: info  - using build directory: /opt/build/repo/.next
11:05:18 AM: info  - Copying "static build" directory
11:05:18 AM: info  - No "exportPathMap" found in "next.config.js". Generating map from "./pages"
11:05:18 AM: info  - Launching 7 workers
11:05:18 AM: info  - Exporting (0/3)
11:05:18 AM: info  - Copying "public" directory
11:05:19 AM: info  - Exporting (3/3)
11:05:19 AM: Export successful. Files written to /opt/build/repo/dist
11:05:19 AM: ​
11:05:19 AM: (build.command completed in 28.6s)
11:05:19 AM: ​
11:05:19 AM: ────────────────────────────────────────────────────────────────
11:05:19 AM:   3. @netlify/plugin-nextjs (onBuild event)                     
11:05:19 AM: ────────────────────────────────────────────────────────────────
11:05:19 AM: ​
11:05:19 AM: ​
11:05:19 AM: ────────────────────────────────────────────────────────────────
11:05:19 AM:   Plugin "@netlify/plugin-nextjs" failed                        
11:05:19 AM: ────────────────────────────────────────────────────────────────
11:05:19 AM: ​
11:05:19 AM:   Error message
11:05:19 AM:   Error: The directory "/opt/build/repo/dist" does not contain a Next.js production build. Perhaps the build command was not run, or you specified the wrong publish directory.
11:05:19 AM:   In most cases it should be set to ".next", unless you have chosen a custom "distDir" in your Next config.
11:05:19 AM:   If you are using "next export" then you should set the environment variable NETLIFY_NEXT_PLUGIN_SKIP to "true".
11:05:19 AM: ​
11:05:19 AM:   Plugin details
11:05:19 AM:   Package:        @netlify/plugin-nextjs
11:05:19 AM:   Version:        4.12.2
11:05:19 AM:   Repository:     git+https://github.com/netlify/netlify-plugin-nextjs.git
11:05:19 AM:   npm link:       https://www.npmjs.com/package/@netlify/plugin-nextjs
11:05:19 AM:   Report issues:  https://github.com/netlify/netlify-plugin-nextjs/issues
11:05:19 AM: ​
11:05:19 AM:   Error location
11:05:19 AM:   In "onBuild" event in "@netlify/plugin-nextjs" from Netlify app
11:05:19 AM:       at checkNextSiteHasBuilt (/.netlify/plugins/node_modules/@netlify/plugin-nextjs/lib/helpers/verification.js:72:16)
11:05:19 AM:       at onBuild (/.netlify/plugins/node_modules/@netlify/plugin-nextjs/lib/index.js:42:50)
11:05:19 AM: ​
11:05:19 AM:   Resolved config
11:05:19 AM:   build:
11:05:19 AM:     command: npm run export
11:05:19 AM:     commandOrigin: config
11:05:19 AM:     environment:
11:05:19 AM:       - NEXT_PRIVATE_TARGET
11:05:19 AM:     publish: /opt/build/repo/dist
11:05:19 AM:     publishOrigin: config
11:05:19 AM:   plugins:
11:05:19 AM:     - inputs: {}
11:05:19 AM:       origin: ui
11:05:19 AM:       package: '@netlify/plugin-nextjs'
11:05:19 AM: Caching artifacts
11:05:19 AM: Started saving node modules
11:05:19 AM: Finished saving node modules
11:05:19 AM: Started saving build plugins
11:05:19 AM: Finished saving build plugins
11:05:19 AM: Started saving yarn cache
11:05:22 AM: Finished saving yarn cache
11:05:22 AM: Started saving pip cache
11:05:22 AM: Finished saving pip cache
11:05:22 AM: Started saving emacs cask dependencies
11:05:22 AM: Finished saving emacs cask dependencies
11:05:22 AM: Started saving maven dependencies
11:05:22 AM: Finished saving maven dependencies
11:05:22 AM: Started saving boot dependencies
11:05:22 AM: Finished saving boot dependencies
11:05:22 AM: Started saving rust rustup cache
11:05:22 AM: Finished saving rust rustup cache
11:05:22 AM: Started saving go dependencies
11:05:22 AM: Finished saving go dependencies
11:05:23 AM: Build failed due to a user error: Build script returned non-zero exit code: 2
11:05:23 AM: Creating deploy upload records
11:05:23 AM: Failing build: Failed to build site
11:05:23 AM: Failed during stage 'building site': Build script returned non-zero exit code: 2 (https://ntl.fyi/exit-code-2)
11:05:23 AM: Finished processing build request in 1m11.941334911s

Hey @korompaiistvan,

This is all expected behaviour. Note that:

  1. You’re using a template that is not officially maintained by Netlify.
  2. The last commit was more than a year old.

About this, since you’re using a template, the first clone is made of the template’s repo.

You got an empty repo because the build didn’t succeed. Once the build succeeds, Netlify pushes the content of the template to your repo.

About the actual error though, I’ve already answered that above.