MODULE_NOT_FOUND

Node.js MODULE_NOT_FOUND fix when path looks right

Programming & Dev Tools Intermediate 👁 0 views 📅 May 25, 2026

Your require path looks correct but Node can't find the module. We'll start with fast checks, then move to deeper causes.

The 30-second fix: Clear the require cache

You've double-checked the path. It's right. But Node still throws MODULE_NOT_FOUND. Before you overthink this, do the fastest thing possible: clear Node's internal module cache.

Open your terminal and run:

node -e "delete require.cache[require.resolve('./your-module')]"

But honestly, that's a hack for development. The real 30-second fix is to restart your Node process. Yes, just kill it with Ctrl+C and run node index.js again. I've seen this catch people who edit a file while the server's running and the old module is still cached. After restart, if the error's gone, you're done.

If it's still there, move on.

The 5-minute fix: Check case sensitivity and npm install

Case sensitivity bites you on Windows

On macOS and Linux, file paths are case-sensitive. On Windows, they're not—most of the time. But Node's module resolver is case-sensitive on all platforms. So if your file is MyModule.js and you write require('./mymodule'), it works on Windows but fails on Linux. And if you're deploying to a server, that's a showstopper.

Check this: Open the file in your editor. Look at the actual filename in the file explorer. Does the casing match exactly? If not, rename the file or the require statement to match. I always use lowercase for filenames to avoid this headache.

Did you actually install the module?

For npm packages (not local files), the most common reason for MODULE_NOT_FOUND is that you didn't run npm install after cloning the repo, or you forgot to add the dependency to package.json.

Run this in your project root:

npm install

Then try running your script again. Still failing? Check if the module name is spelled correctly in your require() and in package.json. A single typo—like expres instead of express—will trigger the error. I've done it, you've done it. Double-check.

Check the node_modules folder

Sometimes npm install says it succeeded but the folder is empty or corrupt. Delete your node_modules folder and run npm install again. On Windows, you might need to use rimraf or npx rimraf node_modules if you get permission errors.

After reinstalling, check that the module's folder exists inside node_modules. If it's missing, the package might have been removed from npm or your registry is pointing to a private one that doesn't have it.

The 15-minute fix: Deep dive into module resolution and path issues

If you've done the steps above and still see MODULE_NOT_FOUND, it's time to understand how Node resolves modules.

Local files vs. npm packages

When you write require('./something'), Node looks for a file named something.js, something.json, or something/index.js in the same directory as the calling file. If the path starts with /, ./, or ../, it's a local file. If it's a bare name like require('express'), Node looks in node_modules folders up the directory tree.

One trap: If you accidentally use a relative path without the dot-slash, like require('myfile'), Node treats it as a package name and won't find it. Always use ./ or ../ for local files.

Check the actual file extension

Node resolves extensions in this order: .js, .json, .node, and then any .jsx, .ts if you're using transpilers. If your file is module.ts but you're not running TypeScript, Node won't find it. Rename to .js or set up the appropriate loader.

Circular dependencies

Rare, but I've seen it: If module A requires module B, and module B requires module A, Node might give you a partially loaded module that's undefined—which can look like MODULE_NOT_FOUND. To check, temporarily remove one of the requires and see if the error changes. If it does, refactor to break the cycle.

Check the NODE_PATH environment variable

Node also looks in directories listed in the NODE_PATH environment variable. If that's set incorrectly, it might skip the local node_modules. Run echo $NODE_PATH (or echo %NODE_PATH% on Windows). If it's set, try unsetting it: unset NODE_PATH (on Linux/macOS) or set NODE_PATH= on Windows, then test again.

Verify your Node.js version

Some modules require a specific Node version. Run node -v and check the module's documentation. If you're on an old version, upgrade. I once spent an hour on a module that silently failed on Node 10 but worked on Node 14. Use nvm to switch versions easily.

Use the --trace-warnings flag

Run your script with:

node --trace-warnings your-script.js

This prints a full stack trace that includes the exact file and line where the require failed. Sometimes the error message says one thing, but the stack trace shows a different file is the culprit. I've caught mismatched require calls this way.

Symlink issues

If you're using npm link or working in a monorepo, symlinks can confuse Node. The module might be installed but Node can't follow the symlink. Run ls -la node_modules/your-module to see if it's a symlink. If it points to a missing directory, re-link or use a regular install.

When all else fails: Start fresh

This is my nuclear option, but it works:

  1. Copy your source files to a new folder (except node_modules and package-lock.json).
  2. Delete the old project folder.
  3. Create a new package.json by running npm init -y.
  4. Reinstall dependencies one by one: npm install express, etc.
  5. Test after each install.

This strips out any corrupted cache, broken symlinks, or misconfiguration. It's overkill 90% of the time, but that 10% saves you hours.

One last thing: If you're using Docker, the build context might not include the right folder. Check your .dockerignore file—I've seen people exclude node_modules accidentally and then wonder why the container can't find modules.

You'll fix this. Start with the cache clear, check casing, then go deeper. Good luck.

Was this solution helpful?