Cannot find module '...'
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.jsonbutnode_modulesis missing (forgot to runnpm install). - The package is not listed in
package.jsonat 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_modulessymlink 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 installThis 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 install3. Check the import / require path
For relative imports, confirm the path is correct:
ls ./helpers/utils.js # verify file exists at the relative pathFix 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.jsIf your script depends on relative paths, changes in the working directory break module resolution.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro