Last modified: Jun 22, 2026

Fix Passport Module Not Found Error

If you are working with authentication in a Node.js application, you might have seen this error: Cannot find module 'passport'. This is a common issue for beginners. It stops your app from starting. But the fix is usually simple. In this article, we will explain why this error happens. We will also show you how to solve it step by step. You will learn how to prevent it in the future.

What Does This Error Mean?

Node.js uses modules to organize code. The require('passport') function tries to load the Passport module. If Node.js cannot find it, it throws the error. This usually means the module is not installed. Or it is installed in the wrong place. Sometimes, the node_modules folder is missing or corrupted.

Common Causes of the Error

Here are the most common reasons for this error:

  • Passport is not installed in your project.
  • You are running the app from the wrong directory.
  • The node_modules folder is deleted or incomplete.
  • You forgot to run npm install after cloning the project.
  • There is a typo in the require statement.

How to Fix the Error

Step 1: Install Passport

First, check if Passport is in your package.json file. If it is missing, install it using npm. Open your terminal in the project root folder. Run this command:


npm install passport

This will download Passport and add it to node_modules. It will also add it to your package.json dependencies.

Step 2: Check Your Working Directory

Make sure you are in the correct folder. Your terminal should be in the same directory as your package.json file. If you run node app.js from a different folder, Node.js will look for modules in the wrong place. Use cd to navigate to your project root.

Step 3: Reinstall All Dependencies

If modules are still missing, delete the node_modules folder and reinstall everything. This is a common fix for many module errors. Run these commands:


rm -rf node_modules
npm install

This will clear all cached modules and install fresh copies. After that, try running your app again.

Step 4: Verify the Require Path

Check your code for typos. The require function should look like this:


// Correct way to require passport
const passport = require('passport');

Make sure the string is exactly 'passport'. Any extra spaces or wrong case will cause the error. If you are using a local file, use the relative path like require('./passport-config').

Example Code with Comments

Let us see a complete example. This simple Express app uses Passport for authentication. We will show the correct setup.


// app.js - A simple Node.js app using Passport
const express = require('express');
const passport = require('passport'); // Require passport module
const app = express();

// Initialize passport
app.use(passport.initialize());

// Define a route
app.get('/', (req, res) => {
  res.send('Hello, user!');
});

// Start server
app.listen(3000, () => {
  console.log('Server running on port 3000');
});

If you run this file without installing Passport, you will see the error. After you run npm install passport, it works fine.


# Output after fixing the error
Server running on port 3000

How to Prevent This Error

To avoid this error in the future, follow these tips:

  • Always run npm install after cloning a project.
  • Use a package.json file to track all dependencies.
  • Do not delete the node_modules folder manually unless you reinstall.
  • Use version control like Git to keep your package.json updated.
  • Check your require statements for spelling mistakes.

Related Error: Cannot Find Module

This error is very similar to other module errors in Node.js. For example, you might see Cannot find module 'express' or Cannot find module 'mongoose'. The fix is the same. You need to install the missing module. For more details, see our guide on how to Fix Node.js Error: Cannot find module. It covers general solutions for all module errors.

Conclusion

The Cannot find module 'passport' error is easy to fix. Most of the time, you just need to install Passport with npm install passport. Always check your working directory and package.json file. If the problem persists, reinstall all dependencies. By following the steps in this article, you can get your Node.js app running smoothly. Remember to keep your modules updated and use proper error handling. Happy coding!