Fix “Cannot Find Module” Error in Node.js: 7 Common Causes

If you’ve been coding in Node.js long enough, you’ve definitely met this frustrating message:

Error: Cannot find module 'xxx'

This is one of the most common Node.js errors, and it happens when Node.js is unable to locate the module you’re trying to load—whether it’s a package from node_modules or a file in your project.

In this guide, I’ll walk you through the 7 most common causes of the “Cannot find module” error in Node.js, why they happen, and how to fix each one with simple code examples.

What Does “Cannot Find Module” Actually Mean?

When Node.js throws this error, it’s telling you: “Hey, I’m trying to load a module, but I can’t find it anywhere.”

Node.js looks for modules in this order:

  1. Core modules (built-in like fs, path, http)
  2. node_modules folder (installed packages)
  3. Relative paths (your own files like ./utils/helper.js)

If Node.js can’t find the module in any of these places, you get the error.

1. The Module Isn’t Installed (Most Common!)

You’re trying to use a package that’s not installed. Simple as that:

// app.js
const express = require('express');

Error

Error: Cannot find module 'express'

Why This Happens

  • You cloned a project but forgot to run npm install
  • You’re working on a new machine
  • Someone added a dependency but didn’t commit package.json
  • You deleted node_modules folder

Fix

Install the missing package:

npm install express

This downloads the express package and saves it to your node_modules folder.

After installing, always check your package.json to confirm:

{
  "dependencies": {
    "express": "^4.18.2"
  }
}

The ^ means “install this version or any newer minor/patch version.”

or you may also run to verify:

npm list express

If nothing shows, the module is missing.

2. Wrong File Path (Typos Are Real!)

Your import path doesn’t match your actual file location.

// app.js
const helper = require('./helper.js');

Error

Error: Cannot find module './helper.js'

But your file is actually at ./utils/helper.js (no ‘s’ in util). Oops!

Why This Happens

  • Wrong folder name or typo
  • Forgetting the dot at the start
  • Adding or forgetting file extensions
  • Wrong relative path depth

Fix

Double-check your folder structure:

src/
├── app.js
├── controllers/
│   └── userController.js
└── utils/
    └── helper.js

Use correct relative path:

// In app.js
const helper = require('./utils/helper');  

That tiny ./ makes all the difference. It tells Node.js: “Start from the current directory, then go into the utils folder.”

or you may use absolute paths to avoid confusion:

const path = require('path');
const helper = require(path.join(__dirname, 'utils', 'helper'));

__dirname gives you the current file’s directory, so no more guessing!

However, if you are in userController.js, you want to import helper.js. The wrong way:

const helper = require('./utils/helper');

The right way:

const helper = require('./utils/helper');

The .. means “go up to the parent directory.” So from controllers/, you go up to src/, then down to utils/.

Understanding ./ vs ../:

  • ./ = current directory
  • ../ = parent directory (one level up)
  • ../../ = two levels up
  • And so on…

3. Module Name vs File Name Confusion

You’re confusing the package name with the file name.

const db = require('database');

Error

Error: Cannot find module 'database'

This is one of the most confusing aspects for beginners. When you use require(), Node.js needs to know: “Am I looking for an npm package or a file you wrote?”

When importing npm packages, you just write the package name with no slashes or dots:

const express = require('express');
const mongoose = require('mongoose');

Notice there’s no ./ or ../ here. Node.js knows to look inside the node_modules folder automatically. When you write require('express'), Node.js thinks: “Okay, let me check the node_modules folder for a package called express.”

When importing your own files, you MUST start with ./ (current directory) or ../ (parent directory):

const db = require('./config/database');

Fix

Let’s say you have this structure:

my-app/
├── app.js
├── config/
│   └── database.js
└── node_modules/
    └── express/

In your app.js, you need both:

// Import npm package (no ./)
const express = require('express');

// Import your file (needs ./)
const db = require('./config/database');

If you forget the ./ on your own file:

const db = require('config/database');

Node.js will search inside node_modules for a package called config and won’t find it. That’s when you get the error.

4. Missing index.js in Folder Imports

You’re trying to import a folder, but there’s no index.js inside.

const utils = require('./utils');

Error

Error: Cannot find module './utils'

Why This Happens

When you import a folder, Node.js automatically looks for index.js inside that folder.

utils/
├── helper.js
└── validator.js

If index.js does not exist, you will hit error.

Fix

Option 1: Create an index.js:

// utils/index.js
module.exports = {
  helper: require('./helper'),
  validator: require('./validator')
};

Now this works:

const utils = require('./utils');
const result = utils.helper.formatDate();

Option 2: Import specific files:

const helper = require('./utils/helper');
const validator = require('./utils/validator');

or 

const { helper, validator } = require('./utils');

5. Missing Exports or Wrong Export Syntax

Sometimes the module exists, but Node cannot find what you exported.

// utils.js

function sum(a, b) {
  return a + b;
}

When import:

const sum = require('./utils'); 

Error

Error: Cannot find module './utils'

Fix

We need to add the module.exports:

function sum(a, b) {
  return a + b;
}

module.exports = sum; 

If using ES Modules:

export default sum;

6. Case Sensitivity Issues (Windows vs Linux/Mac)

Sometimes, your code works on Windows but breaks on Linux/Mac (or vice versa).

// app.js
const config = require('./Config/database');

Your file is config/database.js (lowercase ‘c’), but you wrote Config (uppercase ‘C’).

On Windows: Works fine (case-insensitive)
On Linux/Mac: Error! (case-sensitive)

Why This Happens

Windows file systems (NTFS) are case-insensitive, but Linux/Mac (ext4, APFS) are case-sensitive.

Fix

Always match the exact case:

// If your folder is 'config' (lowercase)
const database = require('./config/database');

7. TypeScript/ES6 Import Issues

You’re mixing CommonJS and ES6 module syntax, or forgot to compile TypeScript.

import express from 'express';

Error

SyntaxError: Cannot use import statement outside a module

Why This Happens

  • Node.js uses CommonJS (require) by default
  • ES6 imports need special configuration
  • TypeScript needs compilation

Fix

For ES6 Modules in Node.js:

Add to package.json:

{
  "type": "module"
}

Now you can use:

import express from 'express';
import { formatDate } from './utils/helper.js';  // Note: .js extension required!

For TypeScript:

Make sure you compile first:

# Install TypeScript
npm install -D typescript

# Compile
npx tsc

# Or use ts-node for development
npm install -D ts-node
npx ts-node src/app.ts

For mixing require and import:

Don’t do this:

const express = require('express');  // CommonJS
import cors from 'cors';              // ES6

Final: Corrupted node_modules or package-lock.json

This happens more than you think — especially after switching branches.

Symptoms:

  • Package is installed but Node cannot load it
  • Installing dependencies gives warnings
  • Old dependencies stuck in cache

Fix:

Just reinstall everything cleanly:

rm -rf node_modules
rm package-lock.json   # optional
npm install

This rebuilds dependencies from scratch.

Lastly, bookmark this article. Next time you hit this error, you’ll solve it in 2 minutes instead of 2 hours!

Useful Resources

Leave a Comment