How do I resolve Cannot find module error using Node.js?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Cannot find module means Node tried to resolve a dependency and failed somewhere in its lookup rules. The fix is usually straightforward once you identify which kind of module you meant to load: a built-in module, a package from node_modules, or a local file referenced by path.
Start with the Resolution Rules
Node resolves imports differently depending on how you wrote them:
- '
require("fs")means a built-in module' - '
require("express")means a package lookup innode_modules' - '
require("./utils")means a local relative file or directory'
That distinction is the first thing to check, because many errors come from writing a package-style import when you meant a file path.
Example:
If utils.js is a file in the current directory, the second line is the correct one.
Verify the Exact Target
A fast debugging trick is require.resolve(). It asks Node where it would load a module from and throws if resolution fails.
That narrows the problem down quickly:
- wrong package name
- wrong relative path
- file missing
- installed in a different working directory than expected
Check Installation and Project Root
If the missing thing is a package, confirm that it is installed in the project where you are running Node:
Many people install dependencies in one directory and run the script from another. Node will search upward from the current file location, not from whichever folder you mentally consider the project root.
If the dependency tree is broken, reinstalling can help:
CommonJS and ESM Differences
Modern Node projects may use either CommonJS or ECMAScript modules. That affects how imports behave.
For CommonJS:
For ESM:
Two common mistakes happen here:
- using
require()in a project configured for ESM - omitting the file extension in ESM when Node expects it
So if your package.json contains "type": "module", revisit the import syntax before assuming the package itself is missing.
Pay Attention to Case and File Names
A path that works on macOS may fail on Linux if the capitalization does not match the real file name. This is common in CI or Docker builds:
Node treats those as different names when the filesystem is case-sensitive.
Local Files Are Not Packages
Another easy trap is expecting Node to infer the path you meant. It will not turn helper into ./helper for you. If the thing lives in your source tree, say so explicitly with ./ or ../.
That single character is responsible for a large share of Cannot find module errors.
Common Pitfalls
- Forgetting
./or../when importing a local file. - Installing a package in a different directory than the script is running from.
- Mixing CommonJS and ESM syntax without checking the
package.jsonmodule type. - Relying on capitalization that only works on a case-insensitive filesystem.
- Deleting the error stack too quickly instead of looking at the exact path Node tried to resolve.
Summary
- First decide whether the missing module is built-in, package-based, or a local file.
- Use
require.resolve()to see exactly what Node can or cannot resolve. - For local files, include
./or../explicitly. - For packages, verify installation in the correct project directory.
- Check ESM versus CommonJS rules and case-sensitive file names before assuming npm is broken.
Related reading
- How do I retrieve an HTML element's actual width and height?
- How do I return the response from an asynchronous call?
- How do I run a single test using Jest?
- How do I serve index.html in subfolders with S3/Cloudfront?
- How do I resolve ClassNotFoundException?
- How do I resolve git saying "Commit your changes or stash them before you can merge"?
- How do I set distance between flexbox items?
- How do I set/unset a cookie with jQuery?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.