nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
66.81k stars 7.55k forks source link

NestJS + SAM issue #13863

Closed thedarkknight197 closed 1 month ago

thedarkknight197 commented 1 month ago

Is there an existing issue for this?

Current behavior

Hi, i have created a new nest project wrapped by a sam application for aws lambda deployment.

I am following this doc for creation lambda.ts file: https://docs.nestjs.com/faq/serverless#example-integration

My sam template:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  sam-nest

Globals:
  Function:
    Timeout: 30

Resources:
  NestFunction:
    Type: AWS::Serverless::Function 
    Properties:
      CodeUri: ./sam-nest/
      Handler: dist/lambda.handler
      Runtime: nodejs20.x
      MemorySize: 1024
      Architectures:
        - x86_64
      Events:
        Nest:
          Type: HttpApi
          Properties:
            Path: /{proxy+}
            Method: any
        RootPath:
          Type: HttpApi
          Properties:
            Path: /
            Method: ANY

Outputs:
  NestApi:
    Description: "API Gateway endpoint URL for Prod stage for a Nest function"
    Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"

lambda.ts

import { NestFactory } from '@nestjs/core';
import serverlessExpress from '@codegenie/serverless-express';
import { Callback, Context, Handler } from 'aws-lambda';
import { AppModule } from './app.module';

let server: Handler;

async function bootstrap(): Promise<Handler> {
  const app = await NestFactory.create(AppModule);
  await app.init();

  const expressApp = app.getHttpAdapter().getInstance();
  return serverlessExpress({ app: expressApp });
}

export const handler: Handler = async (
  event: any,
  context: Context,
  callback: Callback,
) => {
  server = server ?? (await bootstrap());
  return server(event, context, callback);
};

I receive as issue:

2024-08-06T20:28:06.899Z        7fc76f58-b423-4039-9a9d-c085d3ab0b9a    ERROR   Invoke Error    {"errorType":"TypeError","errorMessage":"(0 , serverless_express_1.default) is not a function","stack":["TypeError: (0 , serverless_express_1.default) is not a function","    at bootstrap (/var/task/dist/lambda.js:12:45)","    at async Runtime.handler (/var/task/dist/lambda.js:15:25)"]}
END RequestId: 7fc76f58-b423-4039-9a9d-c085d3ab0b9a

Minimum reproduction code

not have

Steps to reproduce

No response

Expected behavior

answer from /hello endpoint registered in nest application

Package

Other package

No response

NestJS version

10.0.0

Packages versions

"dependencies": {
    "@codegenie/serverless-express": "^4.14.1",
    "@nestjs/common": "^10.0.0",
    "@nestjs/core": "^10.0.0",
    "@nestjs/platform-express": "^10.3.10",
    "aws-lambda": "^1.0.7",
    "aws-serverless-express": "^3.4.0",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.14.1",
    "reflect-metadata": "^0.2.0",
    "rxjs": "^7.8.1"
  },
  "devDependencies": {
    "@nestjs/cli": "^10.0.0",
    "@nestjs/schematics": "^10.0.0",
    "@nestjs/testing": "^10.0.0",
    "@types/express": "^4.17.17",
    "@types/jest": "^29.5.2",
    "@types/node": "^20.3.1",
    "@types/supertest": "^6.0.0",
    "@typescript-eslint/eslint-plugin": "^7.0.0",
    "@typescript-eslint/parser": "^7.0.0",
    "eslint": "^8.42.0",
    "eslint-config-prettier": "^9.0.0",
    "eslint-plugin-prettier": "^5.0.0",
    "jest": "^29.5.0",
    "prettier": "^3.0.0",
    "source-map-support": "^0.5.21",
    "supertest": "^7.0.0",
    "ts-jest": "^29.1.0",
    "ts-loader": "^9.4.3",
    "ts-node": "^10.9.1",
    "tsconfig-paths": "^4.2.0",
    "typescript": "^5.1.3"
  },

Node.js version

20.14.0

In which operating systems have you tested?

Other

No response

micalevisk commented 1 month ago

I've used @codegenie/serverless-express+nestjs just fine so far

thedarkknight197 commented 1 month ago

Missing in tsconfig.json:

esModuleInterop": true
micalevisk commented 1 month ago

I guess you were supposed to do:

import { configure } from '@codegenie/serverless-express';
thedarkknight197 commented 1 month ago

I solved adding in tsconfig.json the esModuleInterop": true. Did you tried with lib and multi app configuration? @micalevisk

micalevisk commented 1 month ago

Did you tried with lib and multi app configuration?

nop