Error: The directory "/opt/build/repo/build" does not contain a Next.js production build

My build worked perfectly. I can run npm run build with no problems at all, but whenever I try and deploy my Next.js project to Netlify I get an odd error. I added simple code today. I think I updated next js but this is it…

Deploy summary

* **Plugin "@netlify/plugin-nextjs" failed**

Error: The directory "/opt/build/repo/build" does not contain a Next.js production build. Perhaps the build command was not run, or you specified the wrong publish directory. In most cases it should be set to ".next", unless you have chosen a custom "distDir" in your Next config. If you are using "next export" then you should set the environment variable NETLIFY_NEXT_PLUGIN_SKIP to "true".

Deploy log

7:40:43 PM: Build ready to start

7:40:45 PM: build-image version: d2c6dbeac570350a387d832f64bc980dc964ad65 (focal)

7:40:45 PM: build-image tag: v4.8.0

7:40:45 PM: buildbot version: d7330f24833f29d0263d28116347ab83094a2561

7:40:45 PM: Fetching cached dependencies

7:40:45 PM: Failed to fetch cache, continuing with build

7:40:45 PM: Starting to prepare the repo for build

7:40:45 PM: No cached dependencies found. Cloning fresh repo

7:40:45 PM: git clone https://github.com/alextseitlin/tatifrank

7:40:48 PM: Preparing Git Reference refs/heads/main

7:40:49 PM: Parsing package.json dependencies

7:40:50 PM: Starting build script

7:40:50 PM: Installing dependencies

7:40:50 PM: Python version set to 2.7

7:40:50 PM: Downloading and installing node v16.15.1...

7:40:51 PM: Downloading https://nodejs.org/dist/v16.15.1/node-v16.15.1-linux-x64.tar.xz...

7:40:51 PM: Computing checksum with sha256sum

7:40:51 PM: Checksums matched!

7:40:54 PM: Now using node v16.15.1 (npm v8.11.0)

7:40:54 PM: Started restoring cached build plugins

7:40:54 PM: Finished restoring cached build plugins

7:40:54 PM: Attempting ruby version 2.7.2, read from environment

7:40:55 PM: Using ruby version 2.7.2

7:40:55 PM: Using PHP version 8.0

7:40:55 PM: No npm workspaces detected

7:40:55 PM: Started restoring cached node modules

7:40:55 PM: Finished restoring cached node modules

7:40:56 PM: Installing NPM modules using NPM version 8.11.0

7:40:56 PM: npm WARN config tmp This setting is no longer used. npm stores temporary files in a special

7:40:56 PM: npm WARN config location in the cache, and they are managed by

7:40:56 PM: npm WARN config [`cacache`](http://npm.im/cacache).

7:40:56 PM: npm WARN config tmp This setting is no longer used. npm stores temporary files in a special

7:40:56 PM: npm WARN config location in the cache, and they are managed by

7:40:56 PM: npm WARN config [`cacache`](http://npm.im/cacache).

7:40:57 PM: npm WARN old lockfile

7:40:57 PM: npm WARN old lockfile The package-lock.json file was created with an old version of npm,

7:40:57 PM: npm WARN old lockfile so supplemental metadata must be fetched from the registry.

7:40:57 PM: npm WARN old lockfile

7:40:57 PM: npm WARN old lockfile This is a one-time fix-up, please be patient...

7:40:57 PM: npm WARN old lockfile

7:41:04 PM: npm WARN deprecated har-validator@5.1.5: this library is no longer supported

7:41:05 PM: npm WARN deprecated uuid@3.4.0: Please upgrade to version 7 or higher. Older versions may use Math.random() in certain circumstances, which is known to be problematic. See https://v8.dev/blog/math-random for details.

7:41:05 PM: npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142

7:41:11 PM: added 468 packages, and audited 469 packages in 15s

7:41:11 PM: 54 packages are looking for funding

7:41:11 PM: run `npm fund` for details

7:41:11 PM: 1 moderate severity vulnerability

7:41:11 PM: To address all issues (including breaking changes), run:

7:41:11 PM: npm audit fix --force

7:41:11 PM: Run `npm audit` for details.

7:41:11 PM: NPM modules installed

7:41:12 PM: npm WARN config tmp This setting is no longer used. npm stores temporary files in a special

7:41:12 PM: npm WARN config location in the cache, and they are managed by

7:41:12 PM: npm WARN config [`cacache`](http://npm.im/cacache).

7:41:12 PM: Started restoring cached go cache

7:41:12 PM: Finished restoring cached go cache

7:41:12 PM: go version go1.16.5 linux/amd64

7:41:12 PM: go version go1.16.5 linux/amd64

7:41:12 PM: Installing missing commands

7:41:12 PM: Verify run directory

7:41:13 PM: ​

7:41:13 PM: ────────────────────────────────────────────────────────────────

7:41:13 PM: Netlify Build

7:41:13 PM: ────────────────────────────────────────────────────────────────

7:41:13 PM: ​

7:41:13 PM: ❯ Version

7:41:13 PM: @netlify/build 27.1.3

7:41:13 PM: ​

7:41:13 PM: ❯ Flags

7:41:13 PM: baseRelDir: true

7:41:13 PM: buildId: 62a3740b9c3445000756fb66

7:41:13 PM: deployId: 62a3740b9c3445000756fb68

7:41:13 PM: ​

7:41:13 PM: ❯ Current directory

7:41:13 PM: /opt/build/repo

7:41:13 PM: ​

7:41:13 PM: ❯ Config file

7:41:13 PM: No config file was defined: using default values.

7:41:13 PM: ​

7:41:13 PM: ❯ Context

7:41:13 PM: production

7:41:13 PM: ​

7:41:13 PM: ❯ Loading plugins

7:41:13 PM: - @netlify/plugin-nextjs@4.8.0 from Netlify app and package.json

7:41:15 PM: ​

7:41:15 PM: ────────────────────────────────────────────────────────────────

7:41:15 PM: 1. @netlify/plugin-nextjs (onPreBuild event)

7:41:15 PM: ────────────────────────────────────────────────────────────────

7:41:15 PM: ​

7:41:15 PM: No Next.js cache to restore.

7:41:15 PM: Netlify configuration property "build.environment.NEXT_PRIVATE_TARGET" value changed.

7:41:15 PM: ​

7:41:15 PM: (@netlify/plugin-nextjs onPreBuild completed in 25ms)

7:41:15 PM: ​

7:41:15 PM: ────────────────────────────────────────────────────────────────

7:41:15 PM: 2. Build command from Netlify app

7:41:15 PM: ────────────────────────────────────────────────────────────────

7:41:15 PM: ​

7:41:15 PM: $ npm run build

7:41:15 PM: npm WARN config tmp This setting is no longer used. npm stores temporary files in a special

7:41:15 PM: npm WARN config location in the cache, and they are managed by

7:41:15 PM: npm WARN config [`cacache`](http://npm.im/cacache).

7:41:15 PM: > build

7:41:15 PM: > next build

7:41:15 PM: info - Loaded env from /opt/build/repo/.env

7:41:16 PM: warn - No build cache found. Please configure build caching for faster rebuilds. Read more: https://nextjs.org/docs/messages/no-cache

7:41:16 PM: info - Checking validity of types...

7:41:16 PM: warn - No ESLint configuration detected. Run next lint to begin setup

7:41:16 PM: info - Creating an optimized production build...

7:41:38 PM: info - Compiled successfully

7:41:38 PM: info - Collecting page data...

7:41:42 PM: info - Generating static pages (0/15)

7:41:42 PM: info - Generating static pages (3/15)

7:41:42 PM: info - Generating static pages (7/15)

7:41:42 PM: info - Generating static pages (11/15)

7:41:42 PM: info - Generating static pages (15/15)

7:41:42 PM: info - Finalizing page optimization...

7:41:42 PM: Page Size First Load JS

7:41:42 PM: ┌ λ / 21.4 kB 107 kB

7:41:42 PM: ├ └ css/e26ca492deb0bc43.css 1.22 kB

7:41:42 PM: ├ /_app 0 B 72.5 kB

7:41:42 PM: ├ ○ /404 2.27 kB 85 kB

7:41:42 PM: ├ └ css/0897eed4ad85b783.css 975 B

7:41:42 PM: ├ ○ /about 2.46 kB 85.2 kB

7:41:42 PM: ├ λ /api/hello 0 B 72.5 kB

7:41:42 PM: ├ ○ /contact 639 B 83.3 kB

7:41:42 PM: ├ ○ /fotografas-vilniuje 3.19 kB 85.9 kB

7:41:42 PM: ├ ○ /how-it-works 4.85 kB 87.5 kB

7:41:42 PM: ├ ○ /offerings 2.78 kB 85.5 kB

7:41:42 PM: ├ ○ /photograph-vilnius 4.75 kB 90.7 kB

7:41:42 PM: ├ ○ /portfolio 3.11 kB 85.8 kB

7:41:42 PM: ├ ○ /portfolio/template 2.79 kB 85.5 kB

7:41:42 PM: ├ ○ /privacy 4.56 kB 87.3 kB

7:41:42 PM: ├ ○ /ru/photograph-vilnius 4.94 kB 90.9 kB

7:41:42 PM: ├ ○ /success 527 B 83.2 kB

7:41:42 PM: ├ ○ /terms 4.71 kB 87.4 kB

7:41:42 PM: └ ○ /testimonials 3.68 kB 86.4 kB

7:41:42 PM: + First Load JS shared by all 72.5 kB

7:41:42 PM: ├ chunks/framework-5f4595e5518b5600.js 42 kB

7:41:42 PM: ├ chunks/main-f65e66e62fc5ca80.js 28.6 kB

7:41:42 PM: ├ chunks/pages/_app-9bb1518c22f7a3ca.js 1.03 kB

7:41:42 PM: ├ chunks/webpack-9b312e20a4e32339.js 836 B

7:41:42 PM: └ css/54ea277b8fc11f73.css 3.4 kB

7:41:42 PM: λ (Server) server-side renders at runtime (uses getInitialProps or getServerSideProps)

7:41:42 PM: ○ (Static) automatically rendered as static HTML (uses no initial props)

7:41:42 PM: ​

7:41:42 PM: (build.command completed in 27.7s)

7:41:42 PM: ​

7:41:42 PM: ────────────────────────────────────────────────────────────────

7:41:42 PM: 3. @netlify/plugin-nextjs (onBuild event)

7:41:42 PM: ────────────────────────────────────────────────────────────────

7:41:42 PM: ​

7:41:42 PM: ​

7:41:42 PM: ────────────────────────────────────────────────────────────────

7:41:42 PM: Plugin "@netlify/plugin-nextjs" failed

7:41:42 PM: ────────────────────────────────────────────────────────────────

7:41:42 PM: ​

7:41:42 PM: Error message

7:41:42 PM: Error: The directory "/opt/build/repo/build" does not contain a Next.js production build. Perhaps the build command was not run, or you specified the wrong publish directory.

7:41:42 PM: In most cases it should be set to ".next", unless you have chosen a custom "distDir" in your Next config.

7:41:42 PM: If you are using "next export" then you should set the environment variable NETLIFY_NEXT_PLUGIN_SKIP to "true".

7:41:42 PM: ​

7:41:42 PM: Plugin details

7:41:42 PM: Package: @netlify/plugin-nextjs

7:41:42 PM: Version: 4.8.0

7:41:42 PM: Repository: git+https://github.com/netlify/netlify-plugin-nextjs.git

7:41:42 PM: npm link: https://www.npmjs.com/package/@netlify/plugin-nextjs

7:41:42 PM: Report issues: https://github.com/netlify/netlify-plugin-nextjs/issues

7:41:42 PM: ​

7:41:42 PM: Error location

7:41:42 PM: In "onBuild" event in "@netlify/plugin-nextjs" from Netlify app and package.json

7:41:42 PM: at checkNextSiteHasBuilt (/node_modules/@netlify/plugin-nextjs/lib/helpers/verification.js:72:16)

7:41:42 PM: at onBuild (/node_modules/@netlify/plugin-nextjs/lib/index.js:40:50)

7:41:42 PM: ​

7:41:44 PM: Creating deploy upload records

7:41:42 PM: Resolved config

7:41:42 PM: build:

7:41:42 PM: command: npm run build

7:41:42 PM: commandOrigin: ui

7:41:42 PM: environment:

7:41:45 PM: Failed during stage 'building site': Build script returned non-zero exit code: 2 (https://ntl.fyi/exit-code-2)

7:41:42 PM: - NEXT_PRIVATE_TARGET

7:41:42 PM: publish: /opt/build/repo/build

7:41:42 PM: publishOrigin: ui

7:41:42 PM: plugins:

7:41:42 PM: - inputs: {}

7:41:42 PM: origin: ui

7:41:42 PM: package: '@netlify/plugin-nextjs'

7:41:43 PM: Caching artifacts

7:41:43 PM: Started saving node modules

7:41:43 PM: Finished saving node modules

7:41:43 PM: Started saving build plugins

7:41:43 PM: Finished saving build plugins

7:41:43 PM: Started saving pip cache

7:41:43 PM: Finished saving pip cache

7:41:43 PM: Started saving emacs cask dependencies

7:41:43 PM: Finished saving emacs cask dependencies

7:41:43 PM: Started saving maven dependencies

7:41:43 PM: Finished saving maven dependencies

7:41:43 PM: Started saving boot dependencies

7:41:43 PM: Finished saving boot dependencies

7:41:43 PM: Started saving rust rustup cache

7:41:43 PM: Finished saving rust rustup cache

7:41:43 PM: Started saving go dependencies

7:41:43 PM: Finished saving go dependencies

7:41:44 PM: Build failed due to a user error: Build script returned non-zero exit code: 2

7:41:45 PM: Failing build: Failed to build site

7:41:45 PM: Finished processing build request in 1m0.309699334s

It was a HELL searching through all the posts I could find but I found a working fix!

Hope it will help others like me

I tried to install Next.js Runtime on my site and it said it was already installed.

Still getting the “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.”

Anything else you did to fix it?

Thanks but what was the fix?

If you provide a site name to look at, we can find a fix.

The pieces I was missing from my netlify.toml were:

[build]
    publish = ".next"

[[plugins]]
  package = "@netlify/plugin-nextjs"

This line in OP’s error log helped me out, since I was previously pointing to /out

Error: The directory ... does not contain a Next.js production build. ...

In most cases it should be set to ".next", unless you have chosen a custom "distDir" in your Next config.