skip to Main Content

"express-session" it’s installed…
The errors are shown in the image…(https://phpout.com/wp-content/uploads/2023/06/7YElD.png)…

// app.js file 
const express = require('express');
const session = require('express-session');   //<---
const partials = require('express-partials');
.
.
const app = express();
.
.

const middleware = [   //<---
  partials(), // allows layouts
  .
  .

  session ({
    secret: process.env.SECRET_KEY, // Set a secret key for securely signing the session ID cookie
    resave: false,
    saveUninitialized: false,
    store: store
  }),

]; 

Need to set the session…

2

Answers


  1. First thought: I can’t see in your file invoking express you have installed and put as required:

    const app = express()
    

    and next

      app.use(
        session({
            secret: process.env.SECRET_KEY, 
            resave: false,
            saveUninitialized: false,
            store: store,
        })
    );
    

    Other thing is to make sure where are your modules installed. Can you check where do you have your node_modules/ directory?

    I mean:

    ./node_modules/
    

    or maybe

    /usr/local/lib/node_modules/
    

    npm install -g express-session installs globally, not locally. Modules are put into /usr/local/lib/node_modules, and puts executable files go to /usr/local/bin.

    npm install express-session installs locally. This installs your package in the current working directory. Node modules go in ./node_modules, executables go in ./node_modules/.bin/. And that means the script you are running needs to be run from the same directory containing node_modules.

    Login or Signup to reply.
  2. you can use this code:

    const express = require('express');
    const session = require('express-session');
    const partials = require('express-partials');
    
    const app = express();  // <-- You need to define your app instance first
    
    // You can use the express-session middleware after you've defined your app
    app.use(session({
        secret: process.env.SECRET_KEY,
        resave: false,
        saveUninitialized: false,
        store: store
    }));
    
    // Then you can define your routes
    app.get('/', function(req, res) {
        // Your route handling code here
    });
    

    From the error shown in the image, it looks like you’re trying to use express-session before you’ve declared it in your code.

    The express-session middleware should be used after creating your app instance but before defining routes. In other words, you need to define the app instance first, then the session middleware, and finally your routes.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search