Last modified: Jun 22, 2026

Fix Passport-Local Module Error

This error stops your Node.js app from running. It occurs when the passport-local package is missing or not installed correctly. This guide shows you how to fix it fast.

You will learn the root cause, installation steps, common mistakes, and debugging techniques. Each section uses short paragraphs and clear examples. Follow along to get your app back online.

Why This Error Happens

The error "Cannot find module 'passport-local'" appears when Node.js cannot locate the package in your project. Node.js looks for it in the node_modules folder. If the folder is missing or the package is not listed in package.json, the error triggers.

Common reasons include:

  • You forgot to run npm install passport-local.
  • The node_modules folder is corrupted or deleted.
  • You are in the wrong directory when running the command.
  • The package version is incompatible with your Node.js version.

This error is common for beginners. It happens often when setting up authentication with Passport.js. Let's fix it step by step.

Step 1: Install the Missing Package

Open your terminal in the project root folder. Run this command:


npm install passport-local

This downloads the package and adds it to node_modules. It also updates package.json and package-lock.json automatically.

After installation, check if the error is gone. Run your app again:


node app.js

If the error disappears, you are done. If not, move to the next step.

Sometimes, you might encounter a different but related error. For more help, read our guide on Fix Node.js Error: Cannot find module for general solutions.

Step 2: Verify Package Installation

Check if passport-local is in your node_modules folder. Run this command:


ls node_modules | grep passport-local

On Windows, use dir instead of ls. If you see the folder, the package is installed. If not, reinstall it.

Also, check your package.json file. Look for this line inside dependencies:


"passport-local": "^1.0.0"

If it is missing, add it manually and run npm install again.

Step 3: Check Your Import Statement

Your code must import the module correctly. Use require like this:


const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;

// Example usage
passport.use(new LocalStrategy(
  function(username, password, done) {
    // Your authentication logic here
    User.findOne({ username: username }, function (err, user) {
      if (err) { return done(err); }
      if (!user) { return done(null, false); }
      if (!user.verifyPassword(password)) { return done(null, false); }
      return done(null, user);
    });
  }
));

Notice the .Strategy at the end. This is a common mistake. If you omit it, Node.js cannot find the module and throws the error.

Important: Always check the spelling and case. Node.js is case-sensitive on most systems. passport-local is all lowercase.

Step 4: Rebuild node_modules

If the package is installed but the error persists, delete node_modules and reinstall everything. Run these commands:


rm -rf node_modules
npm install

On Windows, use rd /s /q node_modules to delete the folder. Then run npm install to rebuild.

This fixes corrupted files or version conflicts. It is a safe and effective solution.

Step 5: Check Node.js and npm Versions

Outdated Node.js or npm can cause module errors. Check your versions:


node -v
npm -v

Update Node.js to the latest LTS version from the official website. Update npm with:


npm install -g npm@latest

Important: Always use a stable Node.js version for production apps. LTS versions are recommended.

Step 6: Use Package Lock File

The package-lock.json file locks exact versions. If it is missing or outdated, run:


npm install --package-lock-only

This regenerates the lock file. Then run npm install again to sync dependencies.

For more detailed troubleshooting, refer to our guide on Fix Node.js Error: Cannot find module.

Example: Full Working Setup

Here is a complete example of a Node.js app using passport-local. It includes comments for clarity.


// app.js
const express = require('express');
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const session = require('express-session');

const app = express();

// Configure session middleware
app.use(session({
  secret: 'your-secret-key',
  resave: false,
  saveUninitialized: false
}));

app.use(passport.initialize());
app.use(passport.session());

// Mock user database
const users = [
  { id: 1, username: 'admin', password: 'password123' }
];

// Define local strategy
passport.use(new LocalStrategy(
  (username, password, done) => {
    const user = users.find(u => u.username === username);
    if (!user) {
      return done(null, false, { message: 'Incorrect username.' });
    }
    if (user.password !== password) {
      return done(null, false, { message: 'Incorrect password.' });
    }
    return done(null, user);
  }
));

// Serialize and deserialize user
passport.serializeUser((user, done) => {
  done(null, user.id);
});

passport.deserializeUser((id, done) => {
  const user = users.find(u => u.id === id);
  done(null, user);
});

// Login route
app.post('/login', passport.authenticate('local', {
  successRedirect: '/dashboard',
  failureRedirect: '/login'
}));

app.get('/dashboard', (req, res) => {
  if (!req.isAuthenticated()) {
    return res.redirect('/login');
  }
  res.send('Welcome to your dashboard!');
});

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

Save this as app.js. Then install dependencies:


npm install express passport passport-local express-session

Run the app:


node app.js

You should see:


Server running on port 3000

If you see the error again, double-check your import statement. Missing .Strategy is the most common cause.

Debugging Tips

If the error still appears, try these tips:

  • Use npm list passport-local to see the installed version.
  • Check for typos in your require statement.
  • Ensure you are in the correct project directory.
  • Clear npm cache with npm cache clean --force.

Important: Never edit node_modules manually. Always use npm commands to manage packages.

For more advanced issues, see our article on Fix Node.js Error: Cannot find module.

Conclusion

The "Cannot find module 'passport-local'" error is easy to fix. Install the package, check your import, and verify your setup. Follow the steps in this guide to resolve it quickly.

Always use npm install to add dependencies. Keep your Node.js and npm updated. With these practices, you will avoid this error in the future.

Now your Node.js app should run without issues. Happy coding!