Deploy error: Module not found: Error: Can't resolve 'webpack' in '/opt/build/repo/node_modules/@next/react-refresh-utils'

11:25:00 PM: Build ready to start
11:25:01 PM: build-image version: d84c79427e8f83c1ba17bcdd7b3fe38059376b68
11:25:01 PM: build-image tag: v3.6.1
11:25:01 PM: buildbot version: df42d44cc6a0381cf657a671baf049c03981bafe
11:25:01 PM: Building without cache
11:25:01 PM: Starting to prepare the repo for build
11:25:03 PM: No cached dependencies found. Cloning fresh repo
11:25:03 PM: git clone git@gitlab.com:pl-starters/nextjs-mui
11:25:08 PM: Preparing Git Reference refs/heads/master
11:25:09 PM: Starting build script
11:25:10 PM: Installing dependencies
11:25:10 PM: Python version set to 2.7
11:25:11 PM: v12.18.0 is already installed.
11:25:11 PM: Now using node v12.18.0 (npm v6.14.4)
11:25:11 PM: Started restoring cached build plugins
11:25:11 PM: Finished restoring cached build plugins
11:25:11 PM: Attempting ruby version 2.7.1, read from environment
11:25:13 PM: Using ruby version 2.7.1
11:25:13 PM: Using PHP version 5.6
11:25:13 PM: Started restoring cached node modules
11:25:13 PM: Finished restoring cached node modules
11:25:13 PM: Installing NPM modules using NPM version 6.14.4
11:25:23 PM: > sharp@0.26.3 install /opt/build/repo/node_modules/sharp
11:25:23 PM: > (node install/libvips && node install/dll-copy && prebuild-install) || (node-gyp rebuild && node install/dll-copy)
11:25:24 PM: info sharp Downloading https://github.com/lovell/sharp-libvips/releases/download/v8.10.0/libvips-8.10.0-linux-x64.tar.br
11:25:26 PM: npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@2.3.2 (node_modules/fsevents):
11:25:26 PM: npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.3.2: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
11:25:26 PM: added 592 packages from 493 contributors and audited 594 packages in 12.611s
11:25:27 PM: 92 packages are looking for funding
11:25:27 PM:   run `npm fund` for details
11:25:27 PM: found 0 vulnerabilities
11:25:27 PM: NPM modules installed
11:25:27 PM: Started restoring cached go cache
11:25:27 PM: Finished restoring cached go cache
11:25:27 PM: go version go1.14.4 linux/amd64
11:25:27 PM: go version go1.14.4 linux/amd64
11:25:27 PM: Installing missing commands
11:25:27 PM: Verify run directory
11:25:28 PM: ​
11:25:28 PM: ────────────────────────────────────────────────────────────────
11:25:28 PM:   Netlify Build                                                 
11:25:28 PM: ────────────────────────────────────────────────────────────────
11:25:28 PM: ​
11:25:28 PM: ❯ Version
11:25:28 PM:   @netlify/build 9.1.3
11:25:28 PM: ​
11:25:28 PM: ❯ Flags
11:25:28 PM:   deployId: 60303abc96063b4266b7d6a8
11:25:28 PM:   mode: buildbot
11:25:28 PM: ​
11:25:28 PM: ❯ Current directory
11:25:28 PM:   /opt/build/repo
11:25:28 PM: ​
11:25:28 PM: ❯ Config file
11:25:28 PM:   No config file was defined: using default values.
11:25:28 PM: ​
11:25:28 PM: ❯ Context
11:25:28 PM:   production
11:25:29 PM: ​
11:25:29 PM: ❯ Installing plugins
11:25:29 PM:    - @netlify/plugin-nextjs@2.0.1
11:25:45 PM: ​
11:25:45 PM: ❯ Loading plugins
11:25:45 PM:    - @netlify/plugin-nextjs@2.0.1 from Netlify app
11:25:45 PM: ​
11:25:45 PM: ────────────────────────────────────────────────────────────────
11:25:45 PM:   1. onPreBuild command from @netlify/plugin-nextjs             
11:25:45 PM: ────────────────────────────────────────────────────────────────
11:25:45 PM: ​
11:25:45 PM: ​
11:25:45 PM: (@netlify/plugin-nextjs onPreBuild completed in 9ms)
11:25:45 PM: ​
11:25:45 PM: ────────────────────────────────────────────────────────────────
11:25:45 PM:   2. Build command from Netlify app                             
11:25:45 PM: ────────────────────────────────────────────────────────────────
11:25:45 PM: ​
11:25:45 PM: $ next build
11:25:46 PM: warn  - No build cache found. Please configure build caching for faster rebuilds. Read more: https://err.sh/next.js/no-cache
11:25:46 PM: info  - Creating an optimized production build...
11:26:26 PM: Failed to compile.
11:26:26 PM: 
11:26:26 PM: ModuleNotFoundError: Module not found: Error: Can't resolve 'webpack' in '/opt/build/repo/node_modules/@next/react-refresh-utils'
11:26:26 PM: > Build error occurred
11:26:26 PM: Error: > Build failed because of webpack errors
11:26:26 PM:     at /opt/build/repo/node_modules/next/dist/build/index.js:15:918
11:26:26 PM:     at runMicrotasks (<anonymous>)
11:26:26 PM:     at processTicksAndRejections (internal/process/task_queues.js:97:5)
11:26:26 PM:     at async /opt/build/repo/node_modules/next/dist/build/tracer.js:3:470
11:26:27 PM: ​
11:26:27 PM: ────────────────────────────────────────────────────────────────
11:26:27 PM:   "build.command" failed                                        
11:26:27 PM: ────────────────────────────────────────────────────────────────
11:26:27 PM: ​
11:26:27 PM:   Error message
11:26:27 PM:   Command failed with exit code 1: next build
11:26:27 PM: ​
11:26:27 PM:   Error location
11:26:27 PM:   In Build command from Netlify app:
11:26:27 PM:   next build
11:26:27 PM: ​
11:26:27 PM:   Resolved config
11:26:27 PM:   build:
11:26:27 PM:     command: next build
11:26:27 PM:     commandOrigin: ui
11:26:27 PM:   plugins:
11:26:27 PM:     - inputs: {}
11:26:27 PM:       origin: ui
11:26:27 PM:       package: '@netlify/plugin-nextjs'
11:26:27 PM: Caching artifacts
11:26:27 PM: Started saving node modules
11:26:27 PM: Finished saving node modules
11:26:27 PM: Started saving build plugins
11:26:27 PM: Finished saving build plugins
11:26:27 PM: Started saving pip cache
11:26:27 PM: Finished saving pip cache
11:26:27 PM: Started saving emacs cask dependencies
11:26:27 PM: Finished saving emacs cask dependencies
11:26:27 PM: Started saving maven dependencies
11:26:27 PM: Finished saving maven dependencies
11:26:27 PM: Started saving boot dependencies
11:26:27 PM: Finished saving boot dependencies
11:26:27 PM: Started saving rust rustup cache
11:26:27 PM: Finished saving rust rustup cache
11:26:27 PM: Started saving go dependencies
11:26:27 PM: Finished saving go dependencies
11:26:30 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
11:26:30 PM: Failing build: Failed to build site
11:26:30 PM: Failed during stage 'building site': Build script returned non-zero exit code: 2
11:26:30 PM: Finished processing build request in 1m28.802392861s

What may the reason?

Hiya, sorry you are having trouble with your build.

This Support Guide contains a ton of useful debugging tips that can likely help you solve your problem :slight_smile:

We also recommend trying to search the forums with the build error you encountered - it’s likely your question was already asked by someone else!

If you are still having problems, please provide as much information as you can on what you have already tried, what your build settings are, your package.json, etc. Thanks!

I have searched the forum… the build works fine on localhost and works on vercel platform but for some reason does not work on Netlify…
I manage to sort out the webpack error but now have different one:

1:41:11 AM: Build ready to start
1:41:12 AM: build-image version: d84c79427e8f83c1ba17bcdd7b3fe38059376b68
1:41:12 AM: build-image tag: v3.6.1
1:41:12 AM: buildbot version: df42d44cc6a0381cf657a671baf049c03981bafe
1:41:12 AM: Building without cache
1:41:12 AM: Starting to prepare the repo for build
1:41:13 AM: No cached dependencies found. Cloning fresh repo
1:41:13 AM: git clone git@gitlab.com:pl-starters/nextjs-mui
1:41:14 AM: Preparing Git Reference refs/heads/master
1:41:16 AM: Starting build script
1:41:16 AM: Installing dependencies
1:41:16 AM: Python version set to 2.7
1:41:18 AM: v12.18.0 is already installed.
1:41:18 AM: Now using node v12.18.0 (npm v6.14.4)
1:41:18 AM: Started restoring cached build plugins
1:41:18 AM: Finished restoring cached build plugins
1:41:18 AM: Attempting ruby version 2.7.1, read from environment
1:41:20 AM: Using ruby version 2.7.1
1:41:20 AM: Using PHP version 5.6
1:41:20 AM: Started restoring cached node modules
1:41:20 AM: Finished restoring cached node modules
1:41:20 AM: Started restoring cached yarn cache
1:41:20 AM: Finished restoring cached yarn cache
1:41:20 AM: Installing yarn at version 1.22.4
1:41:20 AM: Installing Yarn!
1:41:20 AM: > Downloading tarball...
1:41:20 AM: [1/2]: https://yarnpkg.com/downloads/1.22.4/yarn-v1.22.4.tar.gz --> /tmp/yarn.tar.gz.01NvLQbY3G
1:41:20 AM:   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
1:41:20 AM:                                  Dload  Upload   Total   Spent    Left  Speed
1:41:20 AM:   0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
1:41:20 AM: 100    80  100    80    0     0    366      0 --:--:-- --:--:-- --:--:--   366
1:41:21 AM:   0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
1:41:21 AM: 100    93  100    93    0     0    150      0 --:--:-- --:--:-- --:--:--   659
1:41:21 AM: 100   625  100   625    0     0    704      0 --:--:-- --:--:-- --:--:--   704
1:41:21 AM: 100 1215k  100 1215k    0     0   935k      0  0:00:01  0:00:01 --:--:--  935k
1:41:21 AM: [2/2]: https://yarnpkg.com/downloads/1.22.4/yarn-v1.22.4.tar.gz.asc --> /tmp/yarn.tar.gz.01NvLQbY3G.asc
1:41:22 AM: 100    84  100    84    0     0   1529      0 --:--:-- --:--:-- --:--:--  1529
1:41:22 AM: 100    97  100    97    0     0   1179      0 --:--:-- --:--:-- --:--:--  1179
1:41:22 AM: 100   629  100   629    0     0   5522      0 --:--:-- --:--:-- --:--:--  5522
1:41:22 AM:   0  1028    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
1:41:22 AM: 100  1028  100  1028    0     0   5610      0 --:--:-- --:--:-- --:--:-- 1003k
1:41:22 AM: > Verifying integrity...
1:41:22 AM: gpg: Signature made Mon 09 Mar 2020 03:52:13 PM UTC using RSA key ID 69475BAA
1:41:22 AM: gpg: Good signature from "Yarn Packaging <yarn@dan.cx>"
1:41:22 AM: gpg: Note: This key has expired!
1:41:22 AM: Primary key fingerprint: 72EC F46A 56B4 AD39 C907  BBB7 1646 B01B 86E5 0310
1:41:22 AM:      Subkey fingerprint: 6D98 490C 6F1A CDDD 448E  4595 4F77 6793 6947 5BAA
1:41:22 AM: > GPG signature looks good
1:41:22 AM: > Extracting to ~/.yarn...
1:41:22 AM: > Adding to $PATH...
1:41:22 AM: > Successfully installed Yarn 1.22.4! Please open another terminal where the `yarn` command will now be available.
1:41:23 AM: Installing NPM modules using Yarn version 1.22.4
1:41:23 AM: yarn install v1.22.4
1:41:23 AM: [1/4] Resolving packages...
1:41:23 AM: [2/4] Fetching packages...
1:41:33 AM: info fsevents@2.3.2: The platform "linux" is incompatible with this module.
1:41:33 AM: info "fsevents@2.3.2" is an optional dependency and failed compatibility check. Excluding it from installation.
1:41:33 AM: [3/4] Linking dependencies...
1:41:37 AM: [4/4] Building fresh packages...
1:41:41 AM: Done in 17.68s.
1:41:41 AM: NPM modules installed using Yarn
1:41:41 AM: Started restoring cached go cache
1:41:41 AM: Finished restoring cached go cache
1:41:41 AM: go version go1.14.4 linux/amd64
1:41:41 AM: go version go1.14.4 linux/amd64
1:41:41 AM: Installing missing commands
1:41:41 AM: Verify run directory
1:41:43 AM: ​
1:41:43 AM: ────────────────────────────────────────────────────────────────
1:41:43 AM:   Netlify Build                                                 
1:41:43 AM: ────────────────────────────────────────────────────────────────
1:41:43 AM: ​
1:41:43 AM: ❯ Version
1:41:43 AM:   @netlify/build 9.1.3
1:41:43 AM: ​
1:41:43 AM: ❯ Flags
1:41:43 AM:   deployId: 60305aa7e88b4e6e893bf81e
1:41:43 AM:   mode: buildbot
1:41:43 AM: ​
1:41:43 AM: ❯ Current directory
1:41:43 AM:   /opt/build/repo
1:41:43 AM: ​
1:41:43 AM: ❯ Config file
1:41:43 AM:   No config file was defined: using default values.
1:41:43 AM: ​
1:41:43 AM: ❯ Context
1:41:43 AM:   production
1:41:43 AM: ​
1:41:43 AM: ❯ Installing plugins
1:41:43 AM:    - @netlify/plugin-nextjs@2.0.1
1:42:02 AM: ​
1:42:02 AM: ❯ Loading plugins
1:42:02 AM:    - @netlify/plugin-nextjs@2.0.1 from Netlify app
1:42:02 AM: ​
1:42:02 AM: ────────────────────────────────────────────────────────────────
1:42:02 AM:   1. onPreBuild command from @netlify/plugin-nextjs             
1:42:02 AM: ────────────────────────────────────────────────────────────────
1:42:02 AM: ​
1:42:02 AM: ​
1:42:02 AM: (@netlify/plugin-nextjs onPreBuild completed in 8ms)
1:42:02 AM: ​
1:42:02 AM: ────────────────────────────────────────────────────────────────
1:42:02 AM:   2. Build command from Netlify app                             
1:42:02 AM: ────────────────────────────────────────────────────────────────
1:42:02 AM: ​
1:42:02 AM: $ next build
1:42:03 AM: warn  - No build cache found. Please configure build caching for faster rebuilds. Read more: https://err.sh/next.js/no-cache
1:42:03 AM: info  - Creating an optimized production build...
1:42:38 AM: Failed to compile.
1:42:38 AM: 
1:42:38 AM: ModuleNotFoundError: Module not found: Error: Can't resolve 'pnpapi' in '/opt/build/repo/node_modules/pnp-webpack-plugin'
1:42:38 AM: > Build error occurred
1:42:38 AM: Error: > Build failed because of webpack errors
1:42:38 AM:     at /opt/build/repo/node_modules/next/dist/build/index.js:15:918
1:42:38 AM:     at runMicrotasks (<anonymous>)
1:42:38 AM:     at processTicksAndRejections (internal/process/task_queues.js:97:5)
1:42:38 AM:     at async /opt/build/repo/node_modules/next/dist/build/tracer.js:3:470
1:42:38 AM: ​
1:42:38 AM: ────────────────────────────────────────────────────────────────
1:42:38 AM:   "build.command" failed                                        
1:42:38 AM: ────────────────────────────────────────────────────────────────
1:42:38 AM: ​
1:42:38 AM:   Error message
1:42:38 AM:   Command failed with exit code 1: next build
1:42:38 AM: ​
1:42:38 AM:   Error location
1:42:38 AM:   In Build command from Netlify app:
1:42:38 AM:   next build
1:42:38 AM: ​
1:42:38 AM:   Resolved config
1:42:38 AM:   build:
1:42:38 AM:     command: next build
1:42:38 AM:     commandOrigin: ui
1:42:38 AM:   plugins:
1:42:38 AM:     - inputs: {}
1:42:38 AM:       origin: ui
1:42:38 AM:       package: '@netlify/plugin-nextjs'
1:42:38 AM: Caching artifacts
1:42:38 AM: Started saving node modules
1:42:38 AM: Finished saving node modules
1:42:38 AM: Started saving build plugins
1:42:38 AM: Finished saving build plugins
1:42:38 AM: Started saving yarn cache
1:42:38 AM: Finished saving yarn cache
1:42:38 AM: Started saving pip cache
1:42:38 AM: Finished saving pip cache
1:42:38 AM: Started saving emacs cask dependencies
1:42:38 AM: Finished saving emacs cask dependencies
1:42:38 AM: Started saving maven dependencies
1:42:38 AM: Finished saving maven dependencies
1:42:38 AM: Started saving boot dependencies
1:42:38 AM: Finished saving boot dependencies
1:42:38 AM: Started saving rust rustup cache
1:42:38 AM: Finished saving rust rustup cache
1:42:39 AM: Started saving go dependencies
1:42:39 AM: Finished saving go dependencies
1:42:41 AM: Build failed due to a user error: Build script returned non-zero exit code: 2
1:42:41 AM: Failing build: Failed to build site
1:42:41 AM: Failed during stage 'building site': Build script returned non-zero exit code: 2
1:42:41 AM: Finished processing build request in 1m29.215919022s

Somehow it is specific to Netlify, dont understand why…
package.json looks like that:
{
β€œname”: β€œnext-starter”,
β€œversion”: β€œ0.1.0”,
β€œprivate”: true,
β€œscripts”: {
β€œdev”: β€œnext dev”,
β€œbuild”: β€œnext build”,
β€œstart”: β€œnext start”,
β€œout”: β€œnext build && next export”
},
β€œdependencies”: {
β€œ@material-ui/core”: β€œ^4.11.3”,
β€œaxios”: β€œ^0.21.1”,
β€œclsx”: β€œ^1.1.1”,
β€œgray-matter”: β€œ^4.0.2”,
β€œnext”: β€œlatest@canary”,
β€œnext-mdx-remote”: β€œ^2.1.3”,
β€œreact”: β€œ17.0.1”,
β€œreact-dom”: β€œ17.0.1”,
β€œreact-icons”: β€œ^4.2.0”,
β€œrss”: β€œ^1.2.2”,
β€œwebpack”: β€œ^5.23.0”
}
}

Webstorm has no problem with the β€œpnpapi”…

hey oskar1, are you still dealing with this issue? We weren’t sure what to advise previously, but maybe you have been able to make some progress?

Hey perry, thanks for replying.
I managed to deal with above error, however still struggling with correct configuration to deploy next site to Netlify…
When following this guide

and settings in UI

β€œSet the build command to next build
Leave the publish directory blank”

it does not work.

When I set publish directory to β€œout/” it builds fine but I cannot see changes pushed to GtiLab.
When I set build command to next build && next export which is yarn out in my config it works fine…
However don’t understand why the guide is not telling about it… and what is the correct setup.

Also what I understand is I need to install only @netlify/plugin-nextjs in Netlify UI and I don’t need to install this (GitHub - netlify/next-on-netlify: Build and deploy Next.js applications with Server-Side Rendering on Netlify!) plugin in my project…
Correct?

Thanks

Hey @oskar1 , we are still discussing this and not 100% sure what to advise yet. Can you provide us with your netlify.toml file so we can take a closer look at some things?

I ran into the exact same error today when converting a very small project from .tsx to .jsx. Running the project through eslint and fixing the errors resolved the issue. Something to do with this in particular;

import React, { FunctionComponent, ReactElement } from 'react';
import { GetStaticProps, GetStaticPaths } from 'next';
1 Like