typescriptconnect-mongo

connect-mongo error while building with tsc: Cannot find module 'mongoose'


I'm using connect-mongo to store sessions in a typescript project directly with the mongodb driver, without using moongose. I'm initializing it with clientPromise as following:

const store = new MongoStore({
    clientPromise: getMongoPromise(),
    collection: config.get('sessionCollection'),
    touchAfter: 24 * 3600,
    ttl: Number(config.get('sessionDuration')) * 24 * 3600,
});

When I run my app in dev mode (with ts-node --transpile-only src/start.ts) everything works fine, but when I try to compile it using tsc it gives me this error:

$ tsc
node_modules/connect-mongo/src/types.d.ts(11,27): error TS2307: Cannot find module 'mongoose'.
error Command failed with exit code 2.

I'm not using moongose in my app so I don't want to install it just to silence this error. Is there another way to fix it?

Thanks!


Solution

  • Actually I found it, I was missing the @types/connect-mongo dev package. I trusted VSCode that didn't warn me about missing types but I was wrong.

    After installing it the compilation now runs fine:

    yarn add -D @types/connect-mongo