Skip to content
Cannot find module '...'

Cannot find module '...'

DodaTech 2 min read

The Cannot find module error means Node.js cannot locate a package or file you imported. Fix it by running npm install or correcting the import path.

What It Means

Node.js follows a specific order when resolving modules: built-in modules first, then node_modules directories walking up the filesystem, then paths relative to require() or import. If none of these locations contain the requested module, Node throws Cannot find module. The error includes the exact module name and the paths Node searched.

Why It Happens

  • The package is listed in package.json but node_modules is missing (forgot to run npm install).
  • The package is not listed in package.json at all (not installed).
  • The import path is wrong — relative paths (./, ../) don’t point to the correct file.
  • You’re running the script from a different directory than expected.
  • A node_modules symlink is broken (common with monorepos and workspaces).
  • The module exists but the file extension is missing and Node can’t infer it.

How to Fix It

1. Install dependencies

npm install

This reads package.json and installs all listed dependencies into node_modules. If the package isn’t listed yet, install it explicitly:

npm install <package-name>

2. Verify node_modules exists

ls node_modules
ls node_modules/<package-name>

If node_modules is empty or missing, reinstall:

rm -rf node_modules package-lock.json
npm install

3. Check the import / require path

For relative imports, confirm the path is correct:

ls ./helpers/utils.js        # verify file exists at the relative path

Fix the import in your code:

// Wrong
const utils = require('helpers/utils');

// Correct (relative path)
const utils = require('./helpers/utils');

4. Check for missing extensions

Node.js requires explicit extensions for certain file types:

// This may fail
const data = require('./data');

// Add the extension
const data = require('./data.json');

5. Run from the correct directory

Ensure you’re in the project root:

cd /path/to/project
node src/index.js

If your script depends on relative paths, changes in the working directory break module resolution.

Should I commit node_modules to version control?
Generally no — node_modules should be in .gitignore. The package.json and package-lock.json files are the source of truth. Team members run npm install after cloning to generate their own node_modules directory matching the locked versions.
What if the module is installed but still not found?
This happens with hoisted dependency structures (npm workspaces, monorepos) or when a package is installed but at a version that doesn’t expose the subpath you’re requiring. Check the package’s package.json "exports" field and try npm ls <package-name> to see where the package is actually located in the tree.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro