How to fix "Module not found" error when deploying NextJS app to Netlify

I’m trying to deploy my NextJS App to Netlify but it keeps showing the error:

Module not found: Can't resolve '../components/home/home' in '/opt/build/repo/pages' in the build log.

But locally the build command didn’t result in any errors.

My site name is deft-blini-ec8d55.

The full deploy log :

7:05:57 AM: Build ready to start
7:06:04 AM: build-image version: d2c6dbeac570350a387d832f64bc980dc964ad65 (focal)
7:06:04 AM: build-image tag: v4.8.0
7:06:04 AM: buildbot version: 13616ca0f4280331d49967e190a04e108291d30c
7:06:05 AM: Building without cache
7:06:05 AM: Starting to prepare the repo for build
7:06:05 AM: No cached dependencies found. Cloning fresh repo
7:06:05 AM: git clone https://github.com/AlbertSigsbert/albert-portfolio
7:06:05 AM: Preparing Git Reference refs/heads/main
7:06:06 AM: Parsing package.json dependencies
7:06:07 AM: Starting build script
7:06:07 AM: Installing dependencies
7:06:07 AM: Python version set to 2.7
7:06:08 AM: Downloading and installing node v16.15.0...
7:06:08 AM: Downloading https://nodejs.org/dist/v16.15.0/node-v16.15.0-linux-x64.tar.xz...
7:06:08 AM: Computing checksum with sha256sum
7:06:08 AM: Checksums matched!
7:06:11 AM: Now using node v16.15.0 (npm v8.5.5)
7:06:11 AM: Started restoring cached build plugins
7:06:11 AM: Finished restoring cached build plugins
7:06:11 AM: Attempting ruby version 2.7.2, read from environment
7:06:12 AM: Using ruby version 2.7.2
7:06:13 AM: Using PHP version 8.0
7:06:13 AM: No npm workspaces detected
7:06:13 AM: Started restoring cached node modules
7:06:13 AM: Finished restoring cached node modules
7:06:13 AM: Installing NPM modules using NPM version 8.5.5
7:06:23 AM: added 310 packages, and audited 311 packages in 9s
7:06:23 AM: 114 packages are looking for funding
7:06:23 AM:   run `npm fund` for details
7:06:23 AM: found 0 vulnerabilities
7:06:23 AM: NPM modules installed
7:06:24 AM: Started restoring cached go cache
7:06:24 AM: Finished restoring cached go cache
7:06:24 AM: go version go1.16.5 linux/amd64
7:06:24 AM: go version go1.16.5 linux/amd64
7:06:24 AM: Installing missing commands
7:06:24 AM: Verify run directory
7:06:25 AM: ​
7:06:25 AM: ────────────────────────────────────────────────────────────────
7:06:25 AM:   Netlify Build                                                 
7:06:25 AM: ────────────────────────────────────────────────────────────────
7:06:25 AM: ​
7:06:25 AM: ❯ Version
7:06:25 AM:   @netlify/build 27.1.1
7:06:25 AM: ​
7:06:25 AM: ❯ Flags
7:06:25 AM:   baseRelDir: true
7:06:25 AM:   buildId: 6285c225ab08c31b190f429f
7:06:25 AM:   deployId: 6285c225ab08c31b190f42a1
7:06:25 AM: ​
7:06:25 AM: ❯ Current directory
7:06:25 AM:   /opt/build/repo
7:06:25 AM: ​
7:06:25 AM: ❯ Config file
7:06:25 AM:   No config file was defined: using default values.
7:06:25 AM: ​
7:06:25 AM: ❯ Context
7:06:25 AM:   production
7:06:25 AM: ​
7:06:25 AM: ❯ Installing plugins
7:06:25 AM:    - @netlify/plugin-nextjs@4.7.0
7:06:37 AM: ​
7:06:37 AM: ❯ Loading plugins
7:06:37 AM:    - @netlify/plugin-nextjs@4.7.0 from Netlify app
7:06:38 AM: ​
7:06:38 AM: ────────────────────────────────────────────────────────────────
7:06:38 AM:   1. @netlify/plugin-nextjs (onPreBuild event)                  
7:06:38 AM: ────────────────────────────────────────────────────────────────
7:06:38 AM: ​
7:06:38 AM: No Next.js cache to restore.
7:06:38 AM: Netlify configuration property "build.environment.NEXT_PRIVATE_TARGET" value changed.
7:06:38 AM: ​
7:06:38 AM: (@netlify/plugin-nextjs onPreBuild completed in 20ms)
7:06:38 AM: ​
7:06:38 AM: ────────────────────────────────────────────────────────────────
7:06:38 AM:   2. Build command from Netlify app                             
7:06:38 AM: ────────────────────────────────────────────────────────────────
7:06:38 AM: ​
7:06:38 AM: $ npm run build
7:06:39 AM: > albert-portfolio@0.1.0 build
7:06:39 AM: > next build
7:06:39 AM: warn  - No build cache found. Please configure build caching for faster rebuilds. Read more: https://nextjs.org/docs/messages/no-cache
7:06:39 AM: info  - Checking validity of types...
7:06:42 AM: info  - Creating an optimized production build...
7:06:59 AM: Failed to compile.
7:06:59 AM: 
7:06:59 AM: ./pages/index.js
7:06:59 AM: Module not found: Can't resolve '../components/home/home' in '/opt/build/repo/pages'
7:06:59 AM: > Build failed because of webpack errors
7:06:59 AM: ​
7:06:59 AM: ────────────────────────────────────────────────────────────────
7:06:59 AM:   "build.command" failed                                        
7:06:59 AM: ────────────────────────────────────────────────────────────────
7:06:59 AM: ​
7:06:59 AM:   Error message
7:06:59 AM:   Command failed with exit code 1: npm run build (https://ntl.fyi/exit-code-1)
7:06:59 AM: ​
7:06:59 AM:   Error location
7:06:59 AM:   In Build command from Netlify app:
7:06:59 AM:   npm run build
7:06:59 AM: ​
7:06:59 AM:   Resolved config
7:06:59 AM:   build:
7:06:59 AM:     command: npm run build
7:06:59 AM:     commandOrigin: ui
7:06:59 AM:     environment:
7:06:59 AM:       - NEXT_PRIVATE_TARGET
7:06:59 AM:     publish: /opt/build/repo/.next
7:06:59 AM:     publishOrigin: ui
7:06:59 AM:   plugins:
7:06:59 AM:     - inputs: {}
7:06:59 AM:       origin: ui
7:06:59 AM:       package: '@netlify/plugin-nextjs'
7:06:59 AM: Caching artifacts
7:06:59 AM: Started saving node modules
7:06:59 AM: Finished saving node modules
7:06:59 AM: Started saving build plugins
7:06:59 AM: Finished saving build plugins
7:06:59 AM: Started saving pip cache
7:06:59 AM: Finished saving pip cache
7:06:59 AM: Started saving emacs cask dependencies
7:06:59 AM: Finished saving emacs cask dependencies
7:06:59 AM: Started saving maven dependencies
7:06:59 AM: Finished saving maven dependencies
7:06:59 AM: Started saving boot dependencies
7:06:59 AM: Finished saving boot dependencies
7:06:59 AM: Started saving rust rustup cache
7:06:59 AM: Finished saving rust rustup cache
7:06:59 AM: Started saving go dependencies
7:06:59 AM: Finished saving go dependencies
7:07:01 AM: Build failed due to a user error: Build script returned non-zero exit code: 2
7:07:01 AM: Creating deploy upload records
7:07:01 AM: Failing build: Failed to build site
7:07:01 AM: Failed during stage 'building site': Build script returned non-zero exit code: 2 (https://ntl.fyi/exit-code-2)
7:07:01 AM: Finished processing build request in 56.671807643s

I have found the solution to my own problem.
It’s related to Windows case insensitive whereas Netlify uses Linux servers which are!
So, I had to rename all my lowercased named components to uppercase and it worked.

These articles have helped me.

I hope it will help someone in the future.

Hey there, @albertio :wave:

Thanks so much for coming back and sharing those resources! They will definitely be beneficial to future Forums members who encounter something similar.

Happy building :rocket: