'Node JS session errors: express-session deprecated

When running my app.js I get this in the terminal:

express-session deprecated undefined resave option; provide resave option node_modules\admin-bro-expressjs\plugin.js:176:14
express-session deprecated undefined saveUninitialized option; provide saveUninitialized option node_modules\admin-bro-expressjs\plugin.js:176:14
express-session deprecated req.secret; provide secret option node_modules\admin-bro-expressjs\plugin.js:176:14
express-session deprecated req.secret; provide secret option app.js:34:5

this is what I have in my process.env :

SESSION_SECRET = "This is my final project of undergrad!"

when I try to load the page on localhost this is what I get so it won't let me see any of my webpage work:

secret option required for sessions
Error: secret option required for sessions
    at session (C:\Users\lil_s\Downloads\Rich Internet Applications Spring 2021\EAZ6NEJsports\EAZ6NEJsports-master\node_modules\express-session\index.js:200:12)
    at Layer.handle [as handle_request] (C:\Users\lil_s\Downloads\Rich Internet Applications Spring 2021\EAZ6NEJsports\EAZ6NEJsports-master\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (C:\Users\lil_s\Downloads\Rich Internet Applications Spring 2021\EAZ6NEJsports\EAZ6NEJsports-master\node_modules\express\lib\router\index.js:317:13)
    at C:\Users\lil_s\Downloads\Rich Internet Applications Spring 2021\EAZ6NEJsports\EAZ6NEJsports-master\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (C:\Users\lil_s\Downloads\Rich Internet Applications Spring 2021\EAZ6NEJsports\EAZ6NEJsports-master\node_modules\express\lib\router\index.js:335:12)
    at next (C:\Users\lil_s\Downloads\Rich Internet Applications Spring 2021\EAZ6NEJsports\EAZ6NEJsports-master\node_modules\express\lib\router\index.js:275:10)
    at SendStream.error (C:\Users\lil_s\Downloads\Rich Internet Applications Spring 2021\EAZ6NEJsports\EAZ6NEJsports-master\node_modules\serve-static\index.js:121:7)
    at SendStream.emit (events.js:315:20)
    at SendStream.error (C:\Users\lil_s\Downloads\Rich Internet Applications Spring 2021\EAZ6NEJsports\EAZ6NEJsports-master\node_modules\send\index.js:270:17)
    at SendStream.onStatError (C:\Users\lil_s\Downloads\Rich Internet Applications Spring 2021\EAZ6NEJsports\EAZ6NEJsports-master\node_modules\send\index.js:421:12)
    at next (C:\Users\lil_s\Downloads\Rich Internet Applications Spring 2021\EAZ6NEJsports\EAZ6NEJsports-master\node_modules\send\index.js:764:28)
    at C:\Users\lil_s\Downloads\Rich Internet Applications Spring 2021\EAZ6NEJsports\EAZ6NEJsports-master\node_modules\send\index.js:772:23
    at FSReqCallback.oncomplete (fs.js:183:21)

Here is app.js below in full as requested by Deepak on their comment:

const dotenv = require("dotenv").config();
const createError = require("http-errors");
const express = require("express");
const path = require("path");
const cookieParser = require("cookie-parser");
const logger = require("morgan");
const mongoose = require("mongoose");
const session = require("express-session");
const passport = require("passport");
const flash = require("connect-flash");
const Category = require("./models/category");
var MongoStore = require("connect-mongo")(session);
const connectDB = require("./config/db");

const app = express();
require("./config/passport");

// mongodb configuration
connectDB();
// view engine setup
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "ejs");

// admin route
const adminRouter = require("./routes/admin");
app.use("/admin", adminRouter);

app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));
app.use(
    session({
        secret: process.env.SESSION_SECRET,
        resave: false,
        saveUninitialized: false,
        store: new MongoStore({
            mongooseConnection: mongoose.connection,
        }),
        //session expires after 3 hours
        cookie: { maxAge: 60 * 1000 * 60 * 3 },
    })
);
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());

// global variables across routes
app.use(async(req, res, next) => {
    try {
        res.locals.login = req.isAuthenticated();
        res.locals.session = req.session;
        res.locals.currentUser = req.user;
        const categories = await Category.find({}).sort({ title: 1 }).exec();
        res.locals.categories = categories;
        next();
    } catch (error) {
        console.log(error);
        res.redirect("/");
    }
});

// add breadcrumbs
get_breadcrumbs = function(url) {
    var rtn = [{ name: "Home", url: "/" }],
        acc = "", // accumulative url
        arr = url.substring(1).split("/");

    for (i = 0; i < arr.length; i++) {
        acc = i != arr.length - 1 ? acc + "/" + arr[i] : null;
        rtn[i + 1] = {
            name: arr[i].charAt(0).toUpperCase() + arr[i].slice(1),
            url: acc,
        };
    }
    return rtn;
};
app.use(function(req, res, next) {
    req.breadcrumbs = get_breadcrumbs(req.originalUrl);
    next();
});

//routes config
const indexRouter = require("./routes/index");
const productsRouter = require("./routes/products");
const usersRouter = require("./routes/user");
const pagesRouter = require("./routes/pages");
app.use("/products", productsRouter);
app.use("/user", usersRouter);
app.use("/pages", pagesRouter);
app.use("/", indexRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
    next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
    // set locals, only providing error in development
    res.locals.message = err.message;
    res.locals.error = req.app.get("env") === "development" ? err : {};

    // render the error page
    res.status(err.status || 500);
    res.render("error");
});

var port = process.env.PORT || 3000;
app.set("port", port);
app.listen(port, () => {
    console.log("Server running at port " + port);
});

module.exports = app;

If there is anything else you need me to provide to help, please let me know! Thanks in advance everybody!



Solution 1:[1]

I discovered today that repl.it stopped parsing .env files, so you have to add your environment variables to the secrets section instead.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Lee