Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NestJS: Adding cookie-parser causes an error

Tags:

cookies

nestjs

When I run this code:

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import cookieParser from 'cookie-parser';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.use(cookieParser());
  await app.listen(3000);
}
bootstrap();

I get:

(node:28) UnhandledPromiseRejectionWarning: TypeError: cookie_parser_1.default is not a function
    at bootstrap (/usr/src/app/dist/main.js:8:36)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)

If I comment out "app.use(cookieParser());" the problem goes away, but I need the cookie parser.

like image 526
Nick Wiltshire Avatar asked Aug 22 '26 20:08

Nick Wiltshire


2 Answers

I literally found this as I was about to hit submit.

Modify the above to have

import * as cookieParser from 'cookie-parser';

and it works as expected.

like image 111
Nick Wiltshire Avatar answered Aug 27 '26 00:08

Nick Wiltshire


add/change in the tsconfig.json "esModuleInterop": true

like image 37
Vasily Avatar answered Aug 26 '26 23:08

Vasily