jdesboeufs / connect-mongo

MongoDB session store for Express

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NestJS - Doesn't compile.

Xapu1337 opened this issue · comments

commented
  • I'm submitting a ...
    [x] bug report
    [ ] feature request
    [ ] question about the decisions made in the repository
    [ ] question about how to use this project

  • Summary
    Using MongoStore within nestjs crashes the application with the following error:

C:\Users\XapuR\OneDrive\phentix-dashboard\dist\server\main.js:5
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
                                                         ^
TypeError: Cannot read properties of undefined (reading 'create')
    at C:\Users\XapuR\OneDrive\phentix-dashboard\src\server\main.ts:19:25
    at Generator.next (<anonymous>)
    at fulfilled (C:\Users\XapuR\OneDrive\phentix-dashboard\dist\server\main.js:5:58)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
  • Other information (e.g. detailed explanation, stack traces, related issues, suggestions how to fix, links for us to have context, eg. StackOverflow, personal fork, etc.)
import { NestFactory } from '@nestjs/core'
import * as session from 'express-session'
import * as passport from 'passport'
import 'colors'
import { AppModule } from './app.module'
import { computeHash } from '~server/utils/utils'
import MongoStore from 'connect-mongo'
;(async function () {
  const app = await NestFactory.create(AppModule)
  const PORT = process.env.PORT || '3000'
  app.use(
    session({
      secret: computeHash(process.env.COOKIE_SECRET || "we'vebeenreachingyouregardingyourcarsextendedwarranty"),
      cookie: {
        maxAge: 60000 * 60 * 24,
      },
      resave: false,
      saveUninitialized: false,
      store: MongoStore.create({ mongoUrl: process.env.MONGO_URI, touchAfter: 24 * 3600 }),
    })
  )
  app.use(passport.initialize())
  app.use(passport.session())
  await app.listen(PORT, () => console.log(`${'NEST'.blue} ${'|'.gray} ${`Running on port: ${PORT.green}`.white}`.bgBlack))
})()

Cannot see how the stack-trace related to mongo-connect. Check this line first?

const app = await NestFactory.create(AppModule)
commented

Cannot see how the stack-trace related to mongo-connect. Check this line first?

const app = await NestFactory.create(AppModule)

ohh sorry i forgot to mention that the .ts stack trace leads to:
https://github.com/Phentix/phentix-dashboard/blob/29f3d0364d8177cf290c4d7023d534ba18c7db98/src/server/main.ts#L18
(Note: It's commented out removing the comment causes the error again)

having similar issue:

import MongoStore from 'connect-mongo';
...
app.use(
    session({
      name: 'NESTJS_SESSION_ID',
      secret: await config.getSessionSecret(),
      resave: false,
      saveUninitialized: false,
      cookie: {
        maxAge: 60000,
      },
      store: MongoStore.create(await config.getMongoOptions()),
    }),
...

store: MongoStore.create(await config.getMongoOptions()), ^ TypeError: Cannot read property 'create' of undefined

Update: I had to use the 'esModuleInterop' flag set true to allow the code above to work.
I'd prefer to not have it set as it is recommended to be false. This might be in the way MongoStore is exported.

I have this issue as well.

Error:

TypeError: Cannot read properties of undefined (reading 'create')

Code:

app.use(
    session({
      secret: process.env.SECRET,
      store: MongoStore.create({
        mongoUrl: process.env.DB,
        dbName: 'maindb',
        collectionName: 'sessions',
        ttl: 14 * 24 * 60 * 60,
        crypto: {
          secret: process.env.SECRET,
        },
      }),
      resave: true,
      saveUninitialized: true,
      cookie: {
        maxAge: 7 * 24 * 60 * 60 * 1000,
        domain: process.env.COOKIE_DOMAIN,
      },
    }),
  );

Temporary Fix:
It can be fixed by setting the esModuleInterop to true in the tsconfig.json but this is not an optimal solution. (Thanks to @mcdaddytalk )

commented

Having the same issue here.
I have fixed the error by importing it like this:

const MongoStore = require("connect-mongo");

I have no clue if this helps anyone but here you go.

I having same issue,

my app not working because I used import MongoStore from 'connect-mongo',
error message is below

TypeError: Cannot read properties of undefined (reading 'create')

Hi all. Sorry for the late reply. Could you guys please provide your connect-mongo & typescript version? We may have to live with esModuleInterop: true since the underlying express still need it. I have tried to change the tsconfig in example. It failed too. https://github.com/jdesboeufs/connect-mongo/blob/master/example/tsconfig.json#L53

@mingchuno
Hi !
my project using the nest.js, version is below description,

connect-mongo : ^4.6.0,
typescript: 4.3.5

I'm solved the problem by @6lph9 solution

commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

I have the same issue

import { NestFactory } from '@nestjs/core'
import  session from 'express-session'
import  passport from 'passport'
import 'colors'
import { AppModule } from './app.module'
import { computeHash } from '~server/utils/utils'
import MongoStore from 'connect-mongo';

Use esModuleInterop: truie and change your import like above it'll fix the issue.

I have the same problem and since esModuleInterop: true will break other imports in my project, i will switch to redis maybe.