Nick Wiltshire
Nick Wiltshire

Reputation: 745

NestJS: Adding cookie-parser causes an error

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.

Upvotes: 6

Views: 4529

Answers (2)

Vasily
Vasily

Reputation: 361

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

Upvotes: 3

Nick Wiltshire
Nick Wiltshire

Reputation: 745

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.

Upvotes: 29

Related Questions