express-session deprecated req.secret; provide secret option app.js:27:9

Viewed 10914

I typed npm start to run my program but this is the comment that U received in the terminal: express-session deprecated req.secret; provide secret option app.js:27:9. I don't understand how this issue needs to be fixed. This is the code from app.js:27:9

app.use(session({
    store: new FileStore(),
    secret: process.env.SESSION_SECRET,
    resave: false,
    saveUninitialized: true,
    is_logged_in: false,
}))
7 Answers

Make sure you added the SESSION_SECRET in .env file. If yes, then in your app.js, add this

const dotenv = require('dotenv').config()

You need to load all your environment variables. at line one on the server.js file write this...

if (process.env.NODE_ENV !== 'production') { require('dotenv').config() }

If you are on running on Linux server: Add environment variables here

edit /etc/environment

Add:

export SESSION_SECRET="Ssdsd@#e$#Rfe@#$d#$#"

You can check if this is correct created with:

printenv or printenv SESSION_SECRET

..and yes, and in your express:

const dotenv = require('dotenv').config() 

app.use(session({
    secret: process.env.SESSION_SECRET
});

If you added the SESSION_SECRET in your .env file already(SESSION_SECRET='this is my session'),then do require dotenv in your app.js file similar to the following:

const dotenv = require('dotenv').config({path:'./.env'});

mentioning path is important otherwise it's not detecting SESSION_SECRET from .env file.

I had this same issue and the culprit was adding the SESSION_SECRET to my .env file.

I was facing the same problem go checkout wether your .env and .gitignore files are in the same folder where server.js is .

My solution:

Create file: config.js

const cfg = {
    ...,
    secured_key: '06vUSNEzq1z9U476UrMEx7xIOPGYfu2m',
    ...
}

module.exports = cfg;

In app.js

...
const cfg = require('./config');
...

app.use(session({
    secret: cfg.secured_key,
    resave: false,
    saveUninitialized: false
}));
...

This error resolve.

Related