
How to Fix 'Error: Cannot find module' in Node.js Without Reinstalling Everything
WebTools
November 21, 2025
Fix 'Cannot find module' errors in seconds.
You’re stuck. Error: Cannot find module './utils/config' pops up. You didn’t delete anything. The file is right there. But Node.js won’t find it. Let’s fix it now.
This isn’t a missing file. It’s a path mismatch — often caused by incorrect require() syntax, wrong working directory, or a misconfigured package.json. It happens after refactoring, cloning repos, or switching branches. And it kills development flow fast.
—
Examples and Tools
- Node.js: Runtime environment that resolves module paths at runtime
require()function: CommonJS syntax used to import local filespackage.json: Defines project root and entry points — critical for path resolutionprocess.cwd(): Shows the current working directory Node is running from__dirname: Absolute path to the current file — a reliable anchor for local requiresnpx node -e "console.log(process.cwd())": Quick way to check where Node thinks it is
—
Actionable Tips
-
Check the exact path in your
require()statement.
If you wroterequire('./utils/config'), make sure the file is namedconfig.jsorconfig/index.js— notconfig.jsonorConfig.js. Node is case-sensitive and won’t guess extensions. -
Use
__dirnameto anchor your paths.
Replace:
require('./utils/config')
With:
require(path.join(__dirname, './utils/config'))
Then addconst path = require('path');at the top. This ensures the path is always relative to the file, not the terminal. -
Run
npx node -e "console.log(process.cwd())"in your terminal.
Compare that output to where your file actually lives. If they don’t match — you’re running Node from the wrong folder.cdinto your project root and try again. -
Verify your
package.jsonhas a correctmainfield if this is a library.
If you’re importing your own module,main: "index.js"must point to the correct entry file. If it’s wrong, Node can’t resolve dependencies correctly. -
Clear the Node.js module cache if you’re in a dev server (like Express or Next.js).
Restart your dev server. Hot reloaders sometimes hold onto old module references. A full restart clears them. -
Don’t use relative paths across nested folders without a plan.
If you’re requiring fromsrc/services/auth.jstosrc/utils/config.js, and your server runs fromsrc/, the path becomes./utils/config. If it runs from/, it breaks. Use absolute paths viatsconfig.jsonbaseUrl or a build step like Webpack/Babel if this gets messy.
—
What This Means for You
- Faster debugging: Stop guessing file locations — fix the path resolution, not the file.
- Fewer team conflicts: Everyone runs the same code, no matter their terminal directory.
- Cleaner refactors: Switching branches or moving files won’t break imports if you anchor them.
—
Key Takeaways
- ‘Cannot find module’ usually means the path is wrong, not the file missing.
- Always use
__dirname+path.join()for local requires in Node.js. - Run
npx node -e "console.log(process.cwd())"to confirm where Node is executing from.
—
Future Implications
- ES Modules (
import/export) are replacing CommonJS, but path resolution issues persist — they just look different. - Modern frameworks like Next.js and Vite handle this automatically — but legacy apps still rely on
require(). - IDEs like VS Code now auto-suggest paths, but they can still mislead if the working directory is wrong.
Sources
- https://nodejs.org/api/modules.html#modules_all_together
- https://nodejs.org/api/path.html#pathjoinpaths
- https://stackoverflow.com/questions/17575444/node-js-error-cannot-find-module