Solving the problem of cookieParser implementation in NestJS

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?

Answer №1

Make sure to update your tsconfig.json file with the following:

"compilerOptions": {
  "esModuleInterop": true,
}

Additionally, modify your import statement from:

import * as cookieParser from 'cookie-parser';

To the new version:

import cookieParser from 'cookie-parser';

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Efficiently storing a newly shuffled list of tasks into the db.json file using Angular

This is the content of my db.json document { "tasks": [ { "id": 1, "text": "Doctors Appointment", "day": "May 5th at 2:30pm", "reminder": true }, { ...

Exploring the Power of SectionList in Typescript

How should SectionList be properly typed? I am encountering an issue where this code works (taken from the official documentation example): <SectionList renderItem={({item, index}) => <Text key={index}>{item}</Text>} renderSectionHea ...

In order to make Angular function properly, it is crucial that I include app.get("*", [...]); in my server.js file

Recently, I delved into server side JavaScript and embarked on my inaugural project using it. The project entails a command and control server for my own cloud server, operating with angular, Expressjs, and bootstrap. Presently, I am encountering challeng ...

What is the best way to get a server to continuously request a JSON file at regular intervals?

I am currently dealing with updating and displaying real-time information from a third-party JSON file that is approximately 1MB in size. The challenge lies in the fact that this JSON file updates constantly, and my access key may be revoked if users direc ...

What is the process of creating the /dist folder in an unreleased version of an npm package?

Currently working on implementing a pull request for this module: https://github.com/echoulen/react-pull-to-refresh ... It seems that the published module generates the /dist folder in the package.json prepublish npm script. I have my local version of the ...

classes_1.Individual is not a callable

I am facing some difficulties with imports and exports in my self-made TypeScript project. Within the "classes" folder, I have individual files for each class that export them. To simplify usage in code, I created an "index.ts" file that imports all class ...

Creating a TypeScript schema with nested maps and arrays using Dynamoose

I'm currently in the process of developing a schema for a specific example: { "foods": [ { "fruits": [{ "apple": { "color": "red", ...

Retrieve information from an XML document

I have some XML content that looks like this: <Artificial name="Artifical name"> <Machine> <MachineEnvironment uri="environment" /> </Machine> <Mobile>taken phone, test when r1 100m ...

Changing the default headless browser to chrome in Cypress: A step-by-step guide

Currently I am utilizing the Cypress framework with TypeScript for my testing. When I execute the command "npx cypress run," all of my tests are running in headless mode using Electron as the default browser. However, I am interested in running them in C ...

Execute the React Native application on Windows by using the command npx react-native run-windows

I recently created a test application using React Native by running npx react-native init Test --template react-native-template-typescript (https://reactnative.dev/docs/typescript). Everything seemed to be working fine, but I encountered an issue where the ...

The FlatList glides effortlessly in any direction

My FlatList allows me to drag and move it in all directions (up/down/right/left) even though it appears vertically due to styling. The scroll bar still shows horizontally, which I want to disable. How can I achieve this? This is the code snippet for using ...

Empty array being returned by Mongoose after calling the populate() function

I've been struggling for the past 3-4 hours, banging my head against the wall and scouring countless articles here on StackOverflow, but I just can't seem to get my response to populate an array correctly. I'm working with Express.js, Typesc ...

Utilizing numerical values in useParams - A beginner's guide

Trying to access specific data from my json file using an ID, like "http://localhost:3001/pokemons/3", leads to a 404 error. All the data is visible at http://localhost:3001/pokemons. It seems that useParams doesn't want me to use id as a number - q ...

Leveraging cloud functions on Firebase for maximum efficiency

Question: Do you require a backend language when using Firebase Cloud Functions, or can TypeScript alone suffice for coding tasks like creating a matchmaking system? Response: There seems to be some uncertainty on the matter even from ChatGPT himself. Is ...

Managing event date changes in Angular PrimeNG FullCalendar

Is there a way to capture an event when the date of an event is changed? I would like to receive the new date in a function. Is this functionality possible? For example, if I have an event scheduled for 2020-01-01 and I drag it to date 2020-01-10, how can ...

Forwarding parameter data type

I am facing an issue with 2 navigation points leading to the same screen 1. this.router.navigate([this.config.AppTree.App.Module.Details.Path], { state: { data: { id: this.TableId } } }); this.router.navigate([this.config.AppTree.App.Module.Details.Pa ...

What is the method for defining a monkey patched class in a TypeScript declarations file?

Let's say there is a class called X: class X { constructor(); performAction(): void; } Now, we have another class named Y where we want to include an object of class X as a property: class Y { xProperty: X; } How do we go about defining ...

Tips for displaying field options after typing parentheses in TypeScript in Visual Studio Code

Whenever the letter "b" is typed, the suggestion of "bar" appears. However, I would prefer if the suggestions show up immediately after typing the brackets. https://i.stack.imgur.com/OFTO4.png ...

Obtain a tuple of identical length from a function

I'm attempting to create a function that returns a tuple with the same length as the parameter tuple passed to it. Although I tried using generics, I am encountering an error when applying the spread operator on the result. My goal is best illustrate ...

Automate the process of replacing imports in Jest automatically

Currently, I am in the process of setting up a testbench for a more intricate application. One challenge we are facing is that our app needs to call backend code which must be mocked to ensure the testbench runs efficiently. To address this issue, we utili ...