“x” cannot be invoked without “new”

currently following a tutorial from the man Brad Traversy about coding an interactive WebApp with node.js and I’ve run into a problem, that still consists when I paste his github code btw.

const MongoStore = require("connect-mongo")(session)

I’m using connect-mongo to store session data, but the require just doesn’t work and it throws this error:

TypeError: Class constructor MongoStore cannot be invoked without ‘new’

could someone help me with a brief explanation and a block of code to fix this, because I’ve looked through several similar questions on stackoverflow, but didn’t really understand it.

Cheers

Answer

The tutorial can be old. The library API can be changed, I recommend you to use latest docs from https://www.npmjs.com/package/connect-mongo

Also if you can find it, you can install the same version of the connect-mongo used in the tutorial with npm i [email protected]

new usage:

const session = require('express-session');
const MongoStore = require('connect-mongo');

app.use(session({
  secret: 'foo',
  store: MongoStore.create(options)
}));

and instead of new MongoStore(), seems you need to use MongoStore.create(options)