"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
First thought: I can’t see in your file invoking
express
you have installed and put as required:and next
Other thing is to make sure where are your modules installed. Can you check where do you have your
node_modules/
directory?I mean:
or maybe
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 containingnode_modules
.you can use this code:
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.