Failure deploying with NextJS

Hello, I am trying to make a directory change from my portfolio to another portfolio, and when trying to deploy it gives me the error that you see in the image, can someone solve it?

site name: lorenzomunoz.dev

Hi, :wave:t6: thanks for reaching out and welcome back to the Netlify support community. I was able to access your full log here and the issue is your build command. Please refer to line 113 on the log.

Can you try updating your build command to npm run build? Also keep in mind case sensitive also finally I’d encourage you to check out a few resources as this documented quite frequently in our forums. If none of this helps, please share your repo.

1 Like

Hi SamO,

I share with you my repo.

new repo:

present repo:

I believe the command is supposed to be npm run build and not npm next build.

but, the new one is a NextJS project

Hi @Lorensio,

Thanks for following up. Have you tried the build using npm run build? If you could give that a try and see if it works for building the site. Thanks!

Hi @Melvin

Yes, I have already tried that option several times, I leave you some screenshots of how I have it installed. Obviously I have already used the npm run build command before deploying it to Netlify and everything was ok, but now it still doesn’t work to upload it to Netlify.

image
image

The error:

@Lorensio The error is in your own screenshot:

image

You are running run build which doesn’t exist.

Change your Build command to npm run build

You have that specified here in your netlify.toml as run build (which is wrong):

image

Actually, now I take a closer look it’s worse than that.

You have your build script in your package.json set to run build:

image

I suspect you’ve gotten confused and broken it.
You should change that build script back to whatever it was originally, perhaps next build

Basically you want Netlify to execute npm run build (which will run whatever the build script is in your package.json) and you want the build script in your package.json to run whatever the actual build process is.

@nathanmartin ok, so recapitulating, I have changed these two things that you told me, is it okay now?

image

image

@Lorensio I can’t say with any certainty, but it now looks like what I would expect.

Try running it and see what happens!

@nathanmartin this is what is happening

@Lorensio Are you able to provide the full build log?

@nathanmartin

3:37:28 PM: Waiting for other deploys from your team to complete. Check the queue: Netlify App
3:38:36 PM: build-image version: fcb0c1b3ada6d25c1cb58e8bc514f5f23cc14f15 (focal)
3:38:36 PM: buildbot version: 7434bd15586c7bad4a3860d2dbd1a5a8ae8f210d
3:38:37 PM: Fetching cached dependencies
3:38:37 PM: Failed to fetch cache, continuing with build
3:38:37 PM: Starting to prepare the repo for build
3:38:37 PM: No cached dependencies found. Cloning fresh repo
3:38:37 PM: git clone --filter=blob:none GitHub - lorenzo-mm/portofolio-v2
3:38:37 PM: Preparing Git Reference refs/heads/master
3:38:41 PM: Starting to install dependencies
3:38:43 PM: Python version set to 3.8
3:38:44 PM: Attempting Ruby version 2.7.2, read from environment
3:38:44 PM: Using Ruby version 2.7.2
3:38:45 PM: Started restoring cached go cache
3:38:45 PM: Finished restoring cached go cache
3:38:47 PM: go version go1.19.13 linux/amd64
3:38:48 PM: Using PHP version 8.0
3:38:49 PM: Downloading and installing node v18.20.1…
3:38:49 PM: Downloading https://nodejs.org/dist/v18.20.1/node-v18.20.1-linux-x64.tar.xz…
3:38:49 PM: Computing checksum with sha256sum
3:38:50 PM: Checksums matched!
3:38:52 PM: Now using node v18.20.1 (npm v10.5.0)
3:38:52 PM: Enabling Node.js Corepack
3:38:52 PM: Started restoring cached build plugins
3:38:52 PM: Finished restoring cached build plugins
3:38:52 PM: Started restoring cached corepack dependencies
3:38:52 PM: Finished restoring cached corepack dependencies
3:38:52 PM: No npm workspaces detected
3:38:52 PM: Started restoring cached node modules
3:38:52 PM: Finished restoring cached node modules
3:38:52 PM: Installing npm packages using npm version 10.5.0
3:39:03 PM: added 400 packages, and audited 401 packages in 11s
3:39:03 PM: 138 packages are looking for funding
3:39:03 PM: run npm fund for details
3:39:03 PM: found 0 vulnerabilities
3:39:03 PM: npm packages installed
3:39:04 PM: Successfully installed dependencies
3:39:04 PM: Starting build script
3:39:07 PM: Detected 1 framework(s)
3:39:07 PM: β€œnext” at version β€œ14.1.4”
3:39:07 PM: Section completed: initializing
3:39:08 PM: ​
3:39:08 PM: Netlify Build
3:39:08 PM: ────────────────────────────────────────────────────────────────
3:39:08 PM: ​
3:39:08 PM: ❯ Version
3:39:08 PM: @netlify/build 29.39.0
3:39:08 PM: ​
3:39:08 PM: ❯ Flags
3:39:08 PM: baseRelDir: true
3:39:08 PM: buildId: 66154498e4a5ee0008a4421e
3:39:08 PM: deployId: 66154498e4a5ee0008a44220
3:39:08 PM: ​
3:39:08 PM: ❯ Current directory
3:39:08 PM: /opt/build/repo
3:39:08 PM: ​
3:39:08 PM: ❯ Config file
3:39:08 PM: /opt/build/repo/netlify.toml
3:39:08 PM: ​
3:39:08 PM: ❯ Context
3:39:08 PM: production
3:39:08 PM: ​
3:39:08 PM: ❯ Using Next.js Runtime - v5.0.0
3:39:10 PM: ​
3:39:10 PM: @netlify/plugin-nextjs (onPreBuild event)
3:39:10 PM: ────────────────────────────────────────────────────────────────
3:39:10 PM: ​
3:39:10 PM: No Next.js cache to restore
3:39:10 PM: ​
3:39:10 PM: (@netlify/plugin-nextjs onPreBuild completed in 12ms)
3:39:10 PM: ​
3:39:10 PM: build.command from netlify.toml
3:39:10 PM: ────────────────────────────────────────────────────────────────
3:39:10 PM: ​
3:39:10 PM: $ npm run build
3:39:10 PM: > portfolio-v2@0.1.0 build
3:39:10 PM: > next build
3:39:10 PM: :warning: No build cache found. Please configure build caching for faster rebuilds. Read more: https://nextjs.org/docs/messages/no-cache
3:39:11 PM: β–² Next.js 14.1.4
3:39:11 PM: Creating an optimized production build …
3:39:18 PM: Failed to compile.
3:39:18 PM:
3:39:18 PM: ./src/components/Bento/Contact.tsx
3:39:18 PM: Module not found: Can"t resolve β€œ@/…/…/public/Pc.webp”
3:39:18 PM: Module Not Found | Next.js
3:39:18 PM: Import trace for requested module:
3:39:18 PM: ./src/components/BentoContainer.tsx
3:39:18 PM: ./src/components/constant/portfolio.ts
3:39:18 PM: Module not found: Can"t resolve β€œ@/…/…/public/mapfre.png”
3:39:18 PM: Module Not Found | Next.js
3:39:18 PM: Import trace for requested module:
3:39:18 PM: ./src/components/Bento/Projects.tsx
3:39:18 PM: ./src/components/BentoContainer.tsx
3:39:18 PM: ./src/components/constant/portfolio.ts
3:39:18 PM: Module not found: Can"t resolve β€œ@/…/…/public/agecalculator.png”
3:39:18 PM: Module Not Found | Next.js
3:39:18 PM: Import trace for requested module:
3:39:18 PM: ./src/components/Bento/Projects.tsx
3:39:18 PM: ./src/components/BentoContainer.tsx
3:39:18 PM: > Build failed because of webpack errors
3:39:18 PM: ​
3:39:18 PM: @netlify/plugin-nextjs (onEnd event)
3:39:18 PM: ────────────────────────────────────────────────────────────────
3:39:18 PM: ​
3:39:18 PM: ​
3:39:18 PM: (@netlify/plugin-nextjs onEnd completed in 7ms)
3:39:19 PM: Failed during stage β€œbuilding site”: Build script returned non-zero exit code: 2
3:39:19 PM: ​
3:39:19 PM: β€œbuild.command” failed
3:39:19 PM: ────────────────────────────────────────────────────────────────
3:39:19 PM: ​
3:39:19 PM: Error message
3:39:19 PM: Command failed with exit code 1: npm run build (Search results for '"non-zero exit code: 1"' - Netlify Support Forums)
3:39:19 PM: ​
3:39:19 PM: Error location
3:39:19 PM: In build.command from netlify.toml:
3:39:19 PM: npm run build
3:39:19 PM: ​
3:39:19 PM: Resolved config
3:39:19 PM: build:
3:39:19 PM: command: npm run build
3:39:19 PM: commandOrigin: config
3:39:19 PM: publish: /opt/build/repo/build
3:39:19 PM: publishOrigin: config
3:39:19 PM: plugins:
3:39:19 PM: - inputs: {}
3:39:19 PM: origin: config
3:39:19 PM: package: β€œ@netlify/plugin-nextjs”
3:39:19 PM: redirects:
3:39:19 PM: - force: true
3:39:19 PM: from: /_next/image/*
query:
q: β€œ:quality”
url: β€œ:url”
w: β€œ:width”
status: 200
to: /.netlify/images?url=:url&w=:width&q=:quality
redirectsOrigin: config
3:39:19 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
3:39:19 PM: Failing build: Failed to build site
3:39:19 PM: Finished processing build request in 42.811s

Your errors are:

Each of them links to some next.js help documntation:
https://nextjs.org/docs/messages/module-not-found

@nathanmartin I am going to check

@nathanmartin

Well, it works, it’s incredible how even images can cause so much headache and these errors haha. Now I have another error, which is not supposed to find the publish directory.

5:17:12 PM: build-image version: fcb0c1b3ada6d25c1cb58e8bc514f5f23cc14f15 (focal)
5:17:12 PM: buildbot version: 7434bd15586c7bad4a3860d2dbd1a5a8ae8f210d
5:17:12 PM: Fetching cached dependencies
5:17:12 PM: Failed to fetch cache, continuing with build
5:17:12 PM: Starting to prepare the repo for build
5:17:12 PM: No cached dependencies found. Cloning fresh repo
5:17:12 PM: git clone --filter=blob:none GitHub - lorenzo-mm/portofolio-v2
5:17:12 PM: Preparing Git Reference refs/heads/master
5:17:16 PM: Starting to install dependencies
5:17:19 PM: Python version set to 3.8
5:17:19 PM: Attempting Ruby version 2.7.2, read from environment
5:17:20 PM: Using Ruby version 2.7.2
5:17:21 PM: Started restoring cached go cache
5:17:21 PM: Finished restoring cached go cache
5:17:22 PM: go version go1.19.13 linux/amd64
5:17:23 PM: Using PHP version 8.0
5:17:25 PM: Downloading and installing node v18.20.1…
5:17:25 PM: Downloading https://nodejs.org/dist/v18.20.1/node-v18.20.1-linux-x64.tar.xz…
5:17:25 PM: Computing checksum with sha256sum
5:17:25 PM: Checksums matched!
5:17:27 PM: Now using node v18.20.1 (npm v10.5.0)
5:17:27 PM: Enabling Node.js Corepack
5:17:28 PM: Started restoring cached build plugins
5:17:28 PM: Finished restoring cached build plugins
5:17:28 PM: Started restoring cached corepack dependencies
5:17:28 PM: Finished restoring cached corepack dependencies
5:17:28 PM: No npm workspaces detected
5:17:28 PM: Started restoring cached node modules
5:17:28 PM: Finished restoring cached node modules
5:17:28 PM: Installing npm packages using npm version 10.5.0
5:17:40 PM: added 400 packages, and audited 401 packages in 12s
5:17:40 PM: 138 packages are looking for funding
5:17:40 PM: run npm fund for details
5:17:40 PM: found 0 vulnerabilities
5:17:40 PM: npm packages installed
5:17:40 PM: Successfully installed dependencies
5:17:40 PM: Starting build script
5:17:43 PM: Detected 1 framework(s)
5:17:43 PM: β€œnext” at version β€œ14.1.4”
5:17:43 PM: Section completed: initializing
5:17:45 PM: ​
5:17:45 PM: Netlify Build
5:17:45 PM: ────────────────────────────────────────────────────────────────
5:17:45 PM: ​
5:17:45 PM: ❯ Version
5:17:45 PM: @netlify/build 29.39.0
5:17:45 PM: ​
5:17:45 PM: ❯ Flags
5:17:45 PM: baseRelDir: true
5:17:45 PM: buildId: 66155ba1d91f6c0008059997
5:17:45 PM: deployId: 66155ba1d91f6c000805999b
5:17:45 PM: ​
5:17:45 PM: ❯ Current directory
5:17:45 PM: /opt/build/repo
5:17:45 PM: ​
5:17:45 PM: ❯ Config file
5:17:45 PM: /opt/build/repo/netlify.toml
5:17:45 PM: ​
5:17:45 PM: ❯ Context
5:17:45 PM: production
5:17:45 PM: ​
5:17:45 PM: ❯ Using Next.js Runtime - v5.0.0
5:17:46 PM: ​
5:17:46 PM: @netlify/plugin-nextjs (onPreBuild event)
5:17:46 PM: ────────────────────────────────────────────────────────────────
5:17:46 PM: ​
5:17:46 PM: No Next.js cache to restore
5:17:46 PM: ​
5:17:46 PM: (@netlify/plugin-nextjs onPreBuild completed in 10ms)
5:17:46 PM: ​
5:17:46 PM: build.command from netlify.toml
5:17:46 PM: ────────────────────────────────────────────────────────────────
5:17:46 PM: ​
5:17:46 PM: $ npm run build
5:17:46 PM: > portfolio-v2@0.1.0 build
5:17:46 PM: > next build
5:17:47 PM: :warning: No build cache found. Please configure build caching for faster rebuilds. Read more: https://nextjs.org/docs/messages/no-cache
5:17:47 PM: β–² Next.js 14.1.4
5:17:47 PM: Creating an optimized production build …
5:17:54 PM: :blossom: daisyUI 4.9.0
5:17:55 PM: β”œβ”€ :heavy_check_mark:︎ 2 themes added daisyUI themes β€” Tailwind CSS Components ( version 4 update is here )
5:17:55 PM: ╰─ :heart:︎ Support daisyUI project: daisyUI component library - Open Collective
5:17:58 PM: βœ“ Compiled successfully
5:17:58 PM: Linting and checking validity of types …
5:18:00 PM: Collecting page data …
5:18:01 PM: Generating static pages (0/4) …
5:18:02 PM: Generating static pages (1/4)
5:18:02 PM: Generating static pages (2/4)
5:18:02 PM: Generating static pages (3/4)
5:18:02 PM: βœ“ Generating static pages (4/4)
5:18:02 PM: Finalizing page optimization …
5:18:02 PM: Collecting build traces …
5:18:14 PM: Route (app) Size First Load JS
5:18:14 PM: β”Œ β—‹ / 141 B 84.4 kB
5:18:14 PM: β”œ β—‹ /_not-found 0 B 0 B
5:18:14 PM: β”” Ξ» /[locale] 110 kB 201 kB
5:18:14 PM: + First Load JS shared by all 84.3 kB
5:18:14 PM: β”œ chunks/69-74961633e7227153.js 29 kB
5:18:14 PM: β”œ chunks/fd9d1056-ba9bc2733972f0f9.js 53.4 kB
5:18:14 PM: β”” other shared chunks (total) 1.91 kB
5:18:14 PM: Ζ’ Middleware 33 kB
5:18:14 PM: β—‹ (Static) prerendered as static content
5:18:14 PM: Ξ» (Dynamic) server-rendered on demand using Node.js
5:18:14 PM: ​
5:18:14 PM: (build.command completed in 27.5s)
5:18:14 PM: ​
5:18:14 PM: @netlify/plugin-nextjs (onBuild event)
5:18:14 PM: ────────────────────────────────────────────────────────────────
5:18:14 PM: ​
5:18:14 PM: ​
5:18:14 PM: @netlify/plugin-nextjs (onEnd event)
5:18:14 PM: ────────────────────────────────────────────────────────────────
5:18:14 PM: ​
5:18:14 PM: ​
5:18:14 PM: (@netlify/plugin-nextjs onEnd completed in 4ms)
5:18:14 PM: ​
5:18:14 PM: Plugin β€œ@netlify/plugin-nextjs” failed
5:18:14 PM: ────────────────────────────────────────────────────────────────
5:18:14 PM: ​
5:18:14 PM: Error message
5:18:14 PM: Error: Your publish directory was not found at: /opt/build/repo/build, please check your build settings
5:18:14 PM: ​
5:18:14 PM: Plugin details
5:18:14 PM: Package: @netlify/plugin-nextjs
5:18:14 PM: Version: 5.0.0
5:18:14 PM: Repository: git+https://github.com/netlify/next-runtime-minimal.git
5:18:14 PM: npm link: @netlify/plugin-nextjs - npm
5:18:14 PM: Report issues: https://github.com/netlify/next-runtime-minimal/issues
5:18:14 PM: ​
5:18:14 PM: Error location
5:18:14 PM: In β€œonBuild” event in β€œ@netlify/plugin-nextjs” from netlify.toml and package.json
5:18:14 PM: ​
5:18:14 PM: Resolved config
5:18:14 PM: build:
5:18:14 PM: command: npm run build
5:18:14 PM: commandOrigin: config
5:18:14 PM: publish: /opt/build/repo/build
5:18:14 PM: publishOrigin: config
5:18:14 PM: plugins:
5:18:14 PM: - inputs: {}
5:18:14 PM: origin: config
5:18:14 PM: package: β€œ@netlify/plugin-nextjs”
5:18:14 PM: redirects:
5:18:14 PM: - force: true
5:18:14 PM: from: /_next/image/*
query:
q: β€œ:quality”
url: β€œ:url”
w: β€œ:width”
status: 200
to: /.netlify/images?url=:url&w=:width&q=:quality
redirectsOrigin: config
5:18:14 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
5:18:14 PM: Failing build: Failed to build site
5:18:15 PM: Finished processing build request in 1m2.95s
5:18:14 PM: Failed during stage β€œbuilding site”: Build script returned non-zero exit code: 2

@Lorensio Confirm what directory your project builds into, then set that so that Netlify tries to publish the correct folder.

As per your netlify.toml screenshot and that error, you appear to have it set as build, but that doesn’t seem to exist once your build has completed.

The next.js documentation indicates it can be controlled with:

You can also just run your build locally npm run build and see what it creates.

@nathanmartin

Yes, yesterday I was looking at what was wrong and why it gave me those errors. I realized that the publish directory had to be set to .Next, which is the folder where the build is created. And the other mistake, I realized that I had to change the eslint version to one that meets the requirements of eslint-config-next@14.1.4, like β€œ^7.23.0” or β€œ^8.0.0”.

Now everything works correctly and the deployment is done,
thank you for your time and help!!

Cheers :technologist: