Last modified: Jun 24, 2025 By Alexander Williams

Fix Error: Cannot find module 'mongoose'

If you see the error Error: Cannot find module 'mongoose', don't worry. This is a common issue in Node.js. It means your project can't locate the Mongoose package. Here's how to fix it.

Why does this error occur?

The error happens when Node.js cannot find the Mongoose module in your project. This usually occurs for one of these reasons:

  • Mongoose is not installed
  • It's installed in the wrong directory
  • There's a typo in the module name
  • Node_modules is corrupted

Solution 1: Install Mongoose

The simplest fix is to install Mongoose. Run this command in your project directory:


npm install mongoose

This will download Mongoose and add it to your node_modules folder. It will also update your package.json file.

Solution 2: Check your installation path

If Mongoose is already installed, check where it's installed. Your project might be looking in the wrong place. Run:


npm list mongoose

This shows where Mongoose is installed. Make sure it's in your project's node_modules folder.

Solution 3: Verify require statement

Check your code for typos in the require statement. It should look like this:


const mongoose = require('mongoose'); // Correct

Common mistakes include uppercase letters or misspellings like 'Mongoose' or 'mongoos'.

Solution 4: Reinstall node_modules

If the error persists, try deleting and reinstalling all modules:


rm -rf node_modules
npm install

This fixes issues with corrupted dependencies. It's similar to fixing Express module errors.

Solution 5: Check global vs local installation

Mongoose should be installed locally in your project. If you installed it globally, remove the global version and install locally:


npm uninstall -g mongoose
npm install mongoose

Solution 6: Verify Node.js and npm versions

Old Node.js or npm versions can cause module issues. Check your versions:


node -v
npm -v

Update if needed. This also helps with Axios module errors.

Solution 7: Clear npm cache

A corrupted npm cache can cause installation problems. Clear it with:


npm cache clean --force

Then reinstall Mongoose. This often solves various Node.js module errors.

Example: Proper Mongoose setup

Here's a complete example of correct Mongoose usage:


// 1. First install mongoose: npm install mongoose
const mongoose = require('mongoose');

// 2. Connect to MongoDB
mongoose.connect('mongodb://localhost:27017/mydatabase', {
  useNewUrlParser: true,
  useUnifiedTopology: true
});

// 3. Define a schema
const userSchema = new mongoose.Schema({
  name: String,
  age: Number
});

// 4. Create a model
const User = mongoose.model('User', userSchema);

Conclusion

The Cannot find module 'mongoose' error is easy to fix. Most often, you just need to install Mongoose correctly. Follow these steps:

  1. Install Mongoose with npm install mongoose
  2. Check for typos in your code
  3. Verify the installation path
  4. Reinstall if needed

If you still have issues, check our guide on fixing dotenv module errors. Happy coding with Mongoose!