Module not found: Can't resolve './components/Header' in webpack and Next.js: how to fix it
Module not found: Can't resolve './components/Header' means the bundler followed an import and found no file or package there. The usual causes: the file name's case differs from the import (fine on macOS and Windows, fatal on a Linux build server), a wrong relative path or a missing extension, a package that is not in package.json, an @/ alias the bundler does not know, and, for Can't resolve 'fs', Node-only code pulled into the browser bundle.
By Livops team, the people who build and run LivopsUpdated
What people search for, as webpack 5 prints it:
Module not found: Error: Can't resolve './components/Header' in '/app/src'
The same in Next.js:
Module not found: Can't resolve './components/Header'
And the variant for a Node.js module in the browser bundle:
Module not found: Error: Can't resolve 'fs' in '/app/node_modules/some-lib'
This page explains what the bundler was looking for, lists the reasons it did not find it, most common first, gives the fix for each, and says where the error shows on Livops.
What does the error mean?
The message is built in two parts. webpack’s resolver, enhanced-resolve, reports Can't resolve '<what was imported>' in '<folder it looked from>'[4], and webpack puts Module not found: in front of it[3]. So the quoted name is exactly what is written in your import, and the folder is where the importing file lives.
Read the quoted name first:
- A path starting with
./,../or/is a file of yours. Look for it from the folder at the end of the message. - A bare name such as
'swr'or'lodash'is a package, looked for innode_modules. @/or~/is an alias, which only works if the bundler was told about it.'fs','path','crypto'are Node.js modules, which do not exist in the browser.
Next.js now builds with Turbopack by default and still offers webpack with next build --webpack[6]. Its page for this error lists four causes: a package not installed, a file in another folder, a file with different casing, and Node.js-only modules in code that also runs in the browser[5].
Causes, most common first
1. The case of the file name differs
The file is components/header.jsx, the import says ./components/Header. On macOS and Windows that resolves, because their usual file systems (APFS, HFS+, NTFS) are not case sensitive[9]. On Linux it does not. Next.js’s docs: incorrect casing leads to build failures on case-sensitive environments like most Linux-based continuous integration[5]. That is why the error appears only after you push.
Fix: make the import and the file name match, letter for letter. If you want to rename the file, do it with Git, not in the editor. On a case-insensitive file system Git sets core.ignoreCase, and then treats header.jsx and Header.jsx as the same file and keeps the name it already knew[9], so a rename in Finder or Explorer may never reach the repository. Do not flip core.ignoreCase by hand: Git’s docs say changing it can cause unexpected behavior[9].
First see what Git has. git ls-files lists the files in the index[11], with the case they were committed with:
git ls-files src/components | grep -i header
Then rename through a temporary name with git mv, which renames a file and records it[10]:
git mv src/components/header.jsx src/components/Header.tmp.jsx
git mv src/components/Header.tmp.jsx src/components/Header.jsx
git commit -m "Rename header.jsx to Header.jsx"
The same applies to folders: Components/ and components/ are two different folders on the build server.
2. The relative path, the extension or the index file is wrong
The file is somewhere else (../components/Header from a page one level deeper), or the import leaves out something the bundler does not add on its own.
-
Extensions. webpack tries the extensions in
resolve.extensions, by default.js,.jsonand.wasm[1]. A.jsx,.tsor.tsxfile imported without its extension resolves only if the config adds it. Setting the list replaces the default, so keep it with'...'[1]:// webpack.config.js resolve: { extensions: [".tsx", ".ts", ".jsx", "..."], }, -
Folders. An import of a folder resolves to the file named in
resolve.mainFiles, by defaultindex[1]../components/Headerfinds./components/Header/index.js, but not./components/Header/Header.js. -
ES modules. In
.mjsfiles, and in.jsfiles whose nearestpackage.jsonhas"type": "module", webpack requires the file extension and does not resolve folders toindex; otherwise it fails withModule not found[2]. Write./utils.js, not./utils.
3. The package is not installed in the project
The import is 'swr' and there is no swr in node_modules. When you import a package from npm, it has to be installed locally[5]:
npm install swr
git add package.json package-lock.json
git commit -m "Add swr"
It works on your machine but not on the server when the package is there by accident: installed globally, copied by hand, or left over from an older branch. A global install does not help a bundler. npm puts global packages in {prefix}/lib/node_modules on Unix, and its rule of thumb is to install locally what you require() and globally only what you run on the command line[8]. The server installs only what package.json and the lockfile list.
4. An @/ alias the bundler does not know
import Header from "@/components/Header" works in the editor, because the editor reads paths from tsconfig.json or jsconfig.json. The bundler resolves it only if it knows the alias too.
-
Next.js has built-in support for
pathsandbaseUrlintsconfig.jsonandjsconfig.json[6].create-next-appsets up@/*by default[6]. If the alias still fails, check the mapping: each entry inpathsis relative tobaseUrl[6].{ "compilerOptions": { "baseUrl": "src/", "paths": { "@/components/*": ["components/*"] } } } -
Plain webpack does not read
tsconfig.jsonunless you tell it to. Either define the alias inresolve.alias[1], or, from webpack 5.105.0, setresolve.tsconfig: true, which readsbaseUrlandpathsfromtsconfig.jsonand replacestsconfig-paths-webpack-plugin[1]:// webpack.config.js const path = require("path"); module.exports = { resolve: { alias: { "@": path.resolve(__dirname, "src") }, // or, on webpack 5.105.0 and later: // tsconfig: true, }, };
Also check that tsconfig.json or jsconfig.json is committed: an alias defined in a file the server never gets does not exist there.
5. Can’t resolve ‘fs’: Node.js code in the browser bundle
fs, path, crypto and the other Node.js core modules exist on the server, not in the browser. webpack 5 no longer polyfills Node.js core modules automatically[1]. For a module webpack 4 used to polyfill, such as path or crypto, the error adds a BREAKING CHANGE note that suggests resolve.fallback; fs is not on that list[3], so for fs you get the bare message.
Fix in Next.js: keep the code on the server. Node.js-only modules work inside getStaticProps, getStaticPaths and getServerSideProps; a module that imports one and is used by a component that also runs in the browser breaks the build[5]. In the App Router, a file marked 'use client' brings all of its imports into the client bundle[7], so read files in a Server Component and pass the data down as props. To make the mistake obvious, mark server-only files[7]:
// lib/data.js
import "server-only";
import { readFile } from "fs/promises";
Importing that module into a Client Component is then a build-time error that names the problem[7].
Fix in plain webpack, when a library only touches fs in a code path the browser never runs, give it an empty module[1]:
// webpack.config.js
resolve: {
fallback: { fs: false },
},
For path, crypto and the rest, the note in the error names the polyfill package to install if you really need the module in the browser[3].
How to confirm the fix locally
Reproduce the server: a fresh clone on a case-sensitive file system, a clean install, the build.
git clone <your repo> /tmp/check && cd /tmp/check
npm ci
npm run build
A fresh clone catches files that were never committed and packages that exist only in your node_modules. To catch case mistakes on macOS or Windows too, build on Linux in a container:
docker run --rm -v "$PWD":/app -w /app node:22 sh -c "npm ci && npm run build"
On Livops
-
Where it shows. The error stops the build, so the deploy fails as
build_failed, and the previous version keeps running. Livops’s docs put it plainly: runnpm run buildon your machine and it stops in the same place. The exception is cause 1: on macOS or Windows your build may pass, so use the container above. See Build failed. -
Linux. The build runs on Linux, so the case of every file and folder name matters, as in cause 1.
-
Uploaded folders. When you deploy by uploading a folder,
node_modulesis dropped, so a package that exists only in your localnode_modulesdoes not arrive. It must be inpackage.json, as in cause 3. -
The commands are yours to set. Livops detects Next.js, Vite, Create React App, Vue CLI, Angular and others and fills in the install and build commands. Change them in Settings → Install and Settings → Build, or in
livops.json, whereinstallandbuildare applied on every deploy:{ "install": "npm ci", "build": "npm run build" } -
Static or server. A statically built site is free on Livops. A Next.js app that renders pages on the server is deployed as an Application, not a static site. See No site found.
-
The assistant. Next to the failed build, the assistant’s button reads the log and explains what happened and how to fix it.
If the error is Node.js’s own Error: Cannot find module when the app starts, not the bundler’s, see Error: Cannot find module. If a build of an old webpack project stops on error:0308010C, see ERR_OSSL_EVP_UNSUPPORTED. If the site builds and then a refreshed page gives 404, see 404 on page refresh.
Questions
Why does it build on my Mac but fail on the server?
Almost always the case of a file name. macOS and Windows treat Header.jsx and header.jsx as the same file; Linux, where build servers run, does not. Next.js's own docs name incorrect casing as a cause of build failures on Linux-based CI.
I renamed the file but the error stays. Why?
On a case-insensitive disk Git treats header.jsx and Header.jsx as one file and keeps the name it already knew, so a rename that changes only the case may never reach the repository. Check with git ls-files and rename with git mv through a temporary name.
My @/components import works in the editor. Why does the build not find it?
The editor reads paths from tsconfig.json or jsconfig.json; the bundler may not. Next.js reads paths and baseUrl from those files itself. Plain webpack needs resolve.alias, or resolve.tsconfig from webpack 5.105.0.
How do I fix Can't resolve 'fs'?
Keep code that uses fs on the server. fs is a Node.js module and does not exist in the browser, and webpack 5 no longer adds browser versions of Node modules. In Next.js, read files in a Server Component or getStaticProps, and mark server-only files with import 'server-only'. If a library only touches fs optionally, resolve.fallback with fs set to false gives it an empty module.
Why do I need the file extension in some imports?
In .mjs files, and in .js files whose package.json says type module, webpack requires the full file name with its extension and does not look for index files in folders. Write ./utils.js, not ./utils.
Should I install the missing package globally?
No. The bundler looks in the project's node_modules, not in npm's global folder. npm's rule is to install locally what your code imports and globally only what you run on the command line. Install it in the project and commit package.json and the lockfile.
Sources
- webpack — Resolve (webpack.js.org). Checked .
- webpack — Module (Rule.resolve.fullySpecified) (webpack.js.org). Checked .
- webpack source — lib/errors/ModuleNotFoundError.js (raw.githubusercontent.com). Checked .
- enhanced-resolve source — lib/Resolver.js (raw.githubusercontent.com). Checked .
- Next.js Docs — Module Not Found (nextjs.org). Checked .
- Next.js Docs — Installation (nextjs.org). Checked .
- Next.js Docs — Server and Client Components (nextjs.org). Checked .
- npm Docs — folders (docs.npmjs.com). Checked .
- Git source — Documentation/config/core.adoc (core.ignoreCase) (raw.githubusercontent.com). Checked .
- Git Docs — git-mv (git-scm.com). Checked .
- Git Docs — git-ls-files (git-scm.com). Checked .
Put your project online today
Connect GitHub or drop a folder. No card is asked for.
Deploy your siteRelated
- Error: Cannot find module (MODULE_NOT_FOUND) in Node.js: how to fix itError: Cannot find module 'x' with code MODULE_NOT_FOUND, or ERR_MODULE_NOT_FOUND with import, means Node.js looked for a package or file and it was not there. On a server the usual causes are a package missing from package.json, a devDependency the install skipped, a path whose case differs from the file, a start command pointing at a file that was never built, or an ES module import without its .js extension.
- npm failed with return code 1 (npm ERR! code 1): how to fix the buildnpm failed with return code 1, or npm install exited with 1, is npm ERR! code 1 in newer npm: it only says that a script npm ran exited with code 1. The real error is printed above it: usually a compile or type error, a lockfile out of sync with package.json, or a dependency whose install script failed. Find the first error line above code 1, fix that, and the build passes.
- Error: error:0308010C:digital envelope routines::unsupported (ERR_OSSL_EVP_UNSUPPORTED): how to fix itError: error:0308010C:digital envelope routines::unsupported with code ERR_OSSL_EVP_UNSUPPORTED means an old build tool, usually webpack 4 inside an old Create React App, Vue CLI or Next.js, asked Node.js 17 or later for the md4 hash, which OpenSSL 3 no longer allows by default. Upgrade the build tool (webpack 5.61 or later, react-scripts 5); NODE_OPTIONS=--openssl-legacy-provider is only a stopgap.
- Next.js hosting without Vercel: the options comparedNext.js runs on any host that runs Node: next build, then next start, and every feature works, Server Actions and image optimization included. On Livops a repository with next in package.json runs as a server from a push for 39 credits a month (39,000 so'm), and a static export is free. Here is how Vercel, Render, Railway, Fly.io and a VPS compare.
- Free static site hosting in 2026: the free tiers comparedA static site — plain HTML, or a Vite, React or Astro build, or a Lovable export — is free on all five. The differences are the limits: Netlify pauses every site when 300 monthly credits run out, Vercel Hobby is for non-commercial use only, GitHub Pages is not for running a business, and Cloudflare Pages does not count static traffic. On Livops up to 30 sites are free, on your own domain, with no card asked for.
- 404 on page refresh in a React or Vue app (SPA): how to fix itA 404 when you refresh a page or open a link like /about in a React Router or Vue Router app means the static host looked for a file at /about, found none, and never loaded index.html, where the router lives. Fix it on the host by rewriting every route to /index.html (Netlify _redirects, Vercel rewrites, nginx try_files), or switch to hash routing. The price of a rewrite: unknown paths stop being 404s unless the app shows its own.