Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NestJS - Doesn't compile. #446

Open
Xapu1337 opened this issue Nov 1, 2021 · 14 comments
Open

NestJS - Doesn't compile. #446

Xapu1337 opened this issue Nov 1, 2021 · 14 comments

Comments

@Xapu1337
Copy link

Xapu1337 commented Nov 1, 2021

  • 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))
})()
@mingchuno
Copy link
Collaborator

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

const app = await NestFactory.create(AppModule)

@Xapu1337
Copy link
Author

Xapu1337 commented Nov 2, 2021

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)

@mcdaddytalk
Copy link

mcdaddytalk commented Nov 3, 2021

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

@mcdaddytalk
Copy link

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.

@BE-CH
Copy link

BE-CH commented Nov 11, 2021

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 )

@6lph9
Copy link

6lph9 commented Dec 4, 2021

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.

@kaminion
Copy link

kaminion commented Feb 3, 2022

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')

@mingchuno
Copy link
Collaborator

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

@kaminion
Copy link

@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

@stale
Copy link

stale bot commented Apr 28, 2022

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.

@stale stale bot added the wontfix label Apr 28, 2022
@arjunhamdalah
Copy link

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.

@stale stale bot removed the wontfix label Jun 8, 2022
@maikknebel
Copy link

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

@c4nzin
Copy link

c4nzin commented Aug 20, 2024

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.

Thanks!

@DasElias
Copy link

Any updates on this? I face the same issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants