Session: Property 'session' does not exist on type 'Request'

Created on 19 Jun 2019  路  12Comments  路  Source: expressjs/session

hi . after installing express-session I get this error when I'm trying to set or get a session
"Property 'session' does not exist on type 'Request"
I use nodejs with expressjs 4 in vs 2017

node version : v10.15.3

my dependencies

 "dependencies": {
    "axios": "^0.19.0",
    "body-parser": "^1.19.0",
    "cookie-parser": "^1.4.4",
    "debug": "^2.2.0",
    "express": "^4.14.0",
    "express-session": "^1.16.2",
    "pug": "^2.0.0-rc.3",
    "session-file-store": "^1.3.0"
  },
  "devDependencies": {
    "@types/debug": "0.0.30",
    "@types/express": "^4.0.37",
    "@types/express-serve-static-core": "^4.0.50",
    "@types/mime": "^1.3.1",
    "@types/serve-static": "^1.7.32",
    "@types/node": "^6.0.87"
  }

my code

var app = express();
var bodyParser = require('body-parser');
var session = require('express-session');
var cookieParser = require('cookie-parser');
var FileStore = require('session-file-store')(session);

app.use(bodyParser.urlencoded({extended: true})); 
app.use(bodyParser.json());       // to support JSON-encoded bodies
app.use(express.json());       // to support JSON-encoded bodies
app.use(express.urlencoded({ extended: true })); // to support URL-encoded bodies
app.use(cookieParser());
app.use(session({
    secret: 'test',
    resave: false,
    store: new FileStore(),
    saveUninitialized: true,
    cookie: {
        secure: false, //set true later
        maxAge: 60000
    }
}));
app.use(function printSession(req, res, next) {
    console.log('req.session', req.session); //error
    return next();
});
question

Most helpful comment

yes, it's only a typescript error. npm install --save @types/express-session did the job . @iamolegga tnx . just had to wait 3 months :)

All 12 comments

same problem for me req.session undefined in a route function

Get rid of cookieParser...

var cookieParser = require('cookie-parser')
app.use(cookieParser())

Since version 1.5.0, the cookie-parser middleware no longer needs to be used for this module to work. This module now directly reads and writes cookies on req/res. Using cookie-parser may result in issues if the secret is not the same between this module and cookie-parser.

What is your outcome after this modification has been made?

app.use(function printSession(req, res, next) {
    console.log('req.session', req.session); //error
    return next();
})

no..do this...

app.use((req, res, next) => {
    console.log('req.session', req.session)
    next()
})

Get rid of cookieParser...

var cookieParser = require('cookie-parser')
app.use(cookieParser())

Since version 1.5.0, the cookie-parser middleware no longer needs to be used for this module to work. This module now directly reads and writes cookies on req/res. Using cookie-parser may result in issues if the secret is not the same between this module and cookie-parser.

What is your outcome after this modification has been made?

Untitled
still, I get the same error

is this the problem?
var FileStore = require('session-file-store')(session);

can you try another session storage, such as this one...
https://www.npmjs.com/package/connect-redis

var session = require('express-session');
var RedisStore = require('connect-redis')(session);

app.use(session({
    store: new RedisStore(options),
    secret: 'keyboard cat',
    resave: false
}));

is this the problem?
var FileStore = require('session-file-store')(session);

can try another session storage, such as this one...
https://www.npmjs.com/package/connect-redis

var session = require('express-session');
var RedisStore = require('connect-redis')(session);

app.use(session({
    store: new RedisStore(options),
    secret: 'keyboard cat',
    resave: false
}));

i will try to install redis

no luck still same error. I have to mention that session is working fine but I still get this error in vs 2017.
this is my tsconfig.json
{ "compilerOptions": { "module": "commonjs", "target": "es5", "lib": ["es5"], "sourceMap": true }, "exclude": [ "node_modules" ] }

vs 2017 = Visual Studio 2017?

Try setting up a more complete cookie...

cookie: {
   path: '/',
   domain: '.yourdomain.io',
   httpOnly: true,
   secure: process.env.protocol === 'https',
   expires: false,
   maxAge: 2 * 60 * 60 * 1000 // 2 hours
}

vs 2017 = Visual Studio 2017?

Try setting up a more complete cookie...

cookie: {
   path: '/',
   domain: '.yourdomain.io',
   httpOnly: true,
   secure: process.env.protocol === 'https',
   expires: false,
   maxAge: 2 * 60 * 60 * 1000 // 2 hours
}

yes Visual Studio 2017
sorry but still same error

hi . after installing express-session I get this error when I'm trying to set or get a session
"Property 'session' does not exist on type 'Request"
I use nodejs with expressjs 4 in vs 2017

node version : v10.15.3

my dependencies

 "dependencies": {
    "axios": "^0.19.0",
    "body-parser": "^1.19.0",
    "cookie-parser": "^1.4.4",
    "debug": "^2.2.0",
    "express": "^4.14.0",
    "express-session": "^1.16.2",
    "pug": "^2.0.0-rc.3",
    "session-file-store": "^1.3.0"
  },
  "devDependencies": {
    "@types/debug": "0.0.30",
    "@types/express": "^4.0.37",
    "@types/express-serve-static-core": "^4.0.50",
    "@types/mime": "^1.3.1",
    "@types/serve-static": "^1.7.32",
    "@types/node": "^6.0.87"
  }

my code

var app = express();
var bodyParser = require('body-parser');
var session = require('express-session');
var cookieParser = require('cookie-parser');
var FileStore = require('session-file-store')(session);

app.use(bodyParser.urlencoded({extended: true})); 
app.use(bodyParser.json());       // to support JSON-encoded bodies
app.use(express.json());       // to support JSON-encoded bodies
app.use(express.urlencoded({ extended: true })); // to support URL-encoded bodies
app.use(cookieParser());
app.use(session({
    secret: 'test',
    resave: false,
    store: new FileStore(),
    saveUninitialized: true,
    cookie: {
        secure: false, //set true later
        maxAge: 60000
    }
}));
app.use(function printSession(req, res, next) {
    console.log('req.session', req.session); //error
    return next();
});

As I've understood it is only typescript error, isn't it? If so, it's because there is not @types/express-session in your dependencies/devDependencies

yes, it's only a typescript error. npm install --save @types/express-session did the job . @iamolegga tnx . just had to wait 3 months :)

thanks everyone. sounds like a typescript thing. typically the typescript project requires you to install @types/<packagename> to use <packagename>, but that is my outsider understanding as I don't use typescript myself, so would defer to typescript users to support the typescript aspects.

Was this page helpful?
0 / 5 - 0 ratings