TS: Namespace 'global.Express' has no exported member 'Session'

I am a bit stuck here and I’m not sure if anyone will be able to help with how deep I am currently. I am 5hr, 43min. into this video by Ben Awad, where we are switching from MikroORM to TypeORM with a postgres DB.

The errors I am getting trying to run yarn watch is:



(He even gets the const conn one! But gets deleted by deleting the /dist directory??)

Here is the server code for his backend.

The file in question is src/types.ts and this is what I have currently:

import { Request, Response } from 'express';
import { Redis } from 'ioredis';

export type MyContext = {
  req: Request & { session: Express.Session };
  redis: Redis;
  res: Response;
};

Which is EXACTLY what he had at this part of the video. I have this in my IDE freaking out:

Any help is super appreciated! Happy holidays, everyone!

You’re not importing the Express type in that file

Apparently there have been changes in types definition for express-session (SO link).
You should remove caret (^) from @types/express-session in server/package.json to have exact version installed:

"@types/express-session": "1.17.0",
1 Like

Nevermind, I had fixed it! Thanks for the help!

Hi, how did you fix it?

Hey @rstorms, I just ran into the same issue following the same tutorial by Ben Award. If you don’t mind, could you please share the solution with me?

Hey, @vivikyd, I’m still trying to barge through this solution funny enough. I’m unable to start my server from yarn dev after yarn watch. He has it set to read the index.js file directly inside the /dist directory, and I’m not sure how it is working for him.

Hey. Could you share the solution to the session error? Thank You

This is my package.json right now, however, I am stuck about ten seconds after what I posted in this thread.

sooo…
this is my take on this:

import { EntityManager, IDatabaseDriver, Connection } from "@mikro-orm/core";
import { Request, Response } from "express";
import { Session } from "express-session";

export type MyContext = {
  em: EntityManager<any> & EntityManager<IDatabaseDriver<Connection>>;
  req: Request & { session?: Session & { userId?: number } };
  res: Response;
};

seems to eliminate that error

Hello hello,

The code you are talking about is useful for version 1.17.0, now that it is in version 1.17.3 there is a small modification that is made :slight_smile:

You need to do this instead ->

import { Request, Response } from "express";
import { Session, SessionData } from "express-session";
import { Redis } from "ioredis";

export type MyContext = {
  req: Request & {
    session: Session & Partial<SessionData> & { UserID: number };
  };
  redis: Redis;
  res: Response;
};
// UserID -> as you wrote it  (userId, userid, etc...)

This will fix that issue :slight_smile:

Happy coding :slight_smile:

2 Likes