Last modified: Jun 22, 2026

Fix Passport-JWT Module Error

Encountering the error Cannot find module 'passport-jwt' in Node.js is common when setting up authentication with JSON Web Tokens (JWT). This error usually means the module is missing from your project dependencies or is not installed correctly.

In this guide, you will learn step-by-step how to fix this error. We will cover installation, common mistakes, and verification methods. The solutions are simple and work for beginners and experienced developers alike.

What Causes This Error?

The error appears when Node.js cannot locate the passport-jwt package in the node_modules folder. This typically happens because:

  • The package was never installed.
  • The package.json file is missing the dependency.
  • The node_modules folder is corrupted or incomplete.
  • You are running the code from the wrong directory.

Let’s fix each of these issues one by one.

Step 1: Install passport-jwt Correctly

The most direct fix is to install the module. Open your terminal in the root of your project and run:


npm install passport-jwt

This command adds passport-jwt to your node_modules folder and updates package.json automatically. After installation, check that the module appears in your dependencies.


# Check package.json for passport-jwt
cat package.json | grep passport-jwt

If you see a line like "passport-jwt": "^4.0.1", the installation succeeded.

Step 2: Verify Your require Statement

Sometimes the error is due to a typo in the require statement. Ensure you are using the exact module name: passport-jwt. Here is a correct example:


// Correct require statement
const passportJWT = require('passport-jwt');

// Extract JwtStrategy and ExtractJwt from the module
const JwtStrategy = passportJWT.Strategy;
const ExtractJwt = passportJWT.ExtractJwt;

If you wrote passport-jwt with a capital letter or a hyphen in the wrong place, Node.js will throw the error. Always use lowercase and hyphens as shown.

Step 3: Rebuild node_modules

If the module is listed in package.json but still missing, your node_modules folder might be broken. Delete it and reinstall all dependencies:


# Remove node_modules
rm -rf node_modules

# Reinstall all dependencies
npm install

This ensures a clean environment. After running these commands, the passport-jwt module will be restored.

Step 4: Check for Version Conflicts

Version mismatches can also cause this error. For example, if your project uses an older version of passport that is incompatible with passport-jwt, the module may not load. Update both packages together:


# Install latest versions of passport and passport-jwt
npm install passport passport-jwt@latest

After updating, test your code again. If you still see the error, check your package.json for duplicate entries or conflicting versions.

Step 5: Verify the Module Path

Ensure you are running your Node.js script from the correct directory. The require function looks for modules relative to the current working directory. If you are inside a subfolder, Node.js may not find the module. Run your script from the project root:


# Navigate to project root first
cd /path/to/your/project

# Then run your script
node app.js

This simple step often resolves the error for beginners.

Example: Full JWT Authentication Setup

Here is a minimal working example using passport-jwt and passport. This code will help you confirm that the module works after fixing the error.


// app.js
const passport = require('passport');
const passportJWT = require('passport-jwt');

const JwtStrategy = passportJWT.Strategy;
const ExtractJwt = passportJWT.ExtractJwt;

// Define a simple JWT strategy
const opts = {
  jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
  secretOrKey: 'your-secret-key'
};

passport.use(new JwtStrategy(opts, function(jwt_payload, done) {
  // In a real app, find user by jwt_payload.id
  const user = { id: jwt_payload.id, name: 'Test User' };
  if (user) {
    return done(null, user);
  } else {
    return done(null, false);
  }
}));

// Test the strategy
passport.authenticate('jwt', { session: false }, (err, user, info) => {
  if (err) {
    console.log('Error:', err);
  } else if (!user) {
    console.log('No user found');
  } else {
    console.log('Authenticated user:', user);
  }
})();

When you run this code with node app.js, you should see output like:


No user found

This output indicates that the module loaded successfully and the strategy executed without errors.

Additional Troubleshooting Tips

If the error persists after following all steps, try these advanced checks:

  • Clear npm cache: npm cache clean --force and then reinstall.
  • Use a different Node.js version. Sometimes compatibility issues arise with older Node versions. Try Node 14 or later.
  • Check for global installations. If you installed passport-jwt globally, it won't be accessible in your project. Always install locally with npm install --save.

For a broader understanding of module errors, read our guide on how to Fix Node.js Error: Cannot find module. It covers similar issues with other packages.

Conclusion

The Cannot find module 'passport-jwt' error is easy to fix. Start by installing the package correctly with npm install passport-jwt. Verify your require statement and ensure you are in the right directory. If the problem continues, rebuild your node_modules folder and check for version conflicts.

By following these steps, you will have passport-jwt working smoothly in your Node.js project. Always test with a simple script to confirm the module loads without errors. For more Node.js troubleshooting, explore our other guides, including the Fix Node.js Error: Cannot find module article.