Categories
JavaScript Answers

How to fix the ‘passport.js passport.initialize() middleware not in use’ error in Node.js?

Spread the love

Sometimes, we want to fix the ‘passport.js passport.initialize() middleware not in use’ error in Node.js.

In this article, we’ll look at how to fix the ‘passport.js passport.initialize() middleware not in use’ error in Node.js.

How to fix the ‘passport.js passport.initialize() middleware not in use’ error in Node.js?

To fix the ‘passport.js passport.initialize() middleware not in use’ error in Node.js, we should make sure the passport.initialize method is called before other Passport middlewares are added.

For instance, we write

const app = express();

app.use(require('cookie-parser')());
app.use(require('body-parser').urlencoded({
  extended: true
}));
app.use(require('express-session')({
  secret: 'keyboard cat',
  resave: true,
  saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());

to call passport.initialize and call app.use with the returned middleware before we call passport.session.

This way, Passport is initialized before we do anything else with Passport, and we avoid the error.

Conclusion

To fix the ‘passport.js passport.initialize() middleware not in use’ error in Node.js, we should make sure the passport.initialize method is called before other Passport middlewares are added.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *