Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Javascript – how to "redeclare" an identifier?

session is imported from express-session.

I also need to export session as its used by many modules.

Module parse failed: Identifier 'session' has already been declared

How to overcome this (I know redeclaring is not possible but is there an alternative?)?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import { getMongoClient } from '@/api-lib/mongodb';
import MongoStore from 'connect-mongo';
import session from 'express-session';
import { promisifyStore } from 'next-session/lib/compat';


const mongoStore = MongoStore.create({
  clientPromise: getMongoClient(),
  stringify: false,
});

const getSession = session({
  secret: process.env.SESSION_SECRET,
  resave: true,
  rolling: true,
  saveUninitialized: false,
  store: promisifyStore(mongoStore),
  cookie: {
    secure: process.env.NODE_ENV == "production" ? false : false ,
    maxAge: 1000 * 60 * 60 * 24 * 7, // 7 days
    sameSite: 'strict',
    //expires: new Date(Date.now() + (1000 * 60 * 60 * 24 * 7)),//7 days
  }
});

export default async function session(req, res, next) {
  await getSession(req, res);
  next();
}

>Solution :

Two ways:

Rename the imported session

import expressSession from 'express-session';
...
const getSession = expressSession({...});

Export an anonymous function

export default async function(req, res, next) {
  await getSession(req, res);
  next();
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading