Greetings! I have a question for you.
Currently, I am working on developing a backend using NestJS, a Node.js framework. Everything is functioning smoothly except for some issues when it comes to hosting.
I have created a new NestJS project and made some modifications in main.ts as shown below.
main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import * as cookieParser from 'cookie-parser'; // This is where the problem lies.
async function bootstrap() {
const app = await NestFactory.create(AppModule, { bodyParser: true });
app.enableCors({
credentials: true,
origin: [
'http://localhost:3000',
'http://localhost:3001',
],
});
app.use(cookieParser());
await app.listen(3002);
}
bootstrap();
The issue that has been causing me a lot of trouble involves importing cookieParser from the 'cookie-parser' module. The NestJS documentation suggests using import * as cookieParser from 'cookie-parser', which works fine locally. However, when deploying on vercel for testing, it results in a This Serverless Function has crashed error. enter image description here
To resolve this, I tried changing the import statement to: import cookieParser from 'cookie-parser' This solution works on vercel but not locally. enter image description here
Additionally, I tested this code on Bluehost, my production server, and found that it only works with import * as cookieParser from 'cookie-parser'.
I am confused and unsure about which approach is correct. Can you provide guidance on how to make it work both locally, on the test host, and on the production host?