CodeGenieApp / serverless-express

Run Express and other Node.js frameworks on AWS Serverless technologies such as Lambda, API Gateway, Lambda@Edge, and more.
https://codegenie.codes
Apache License 2.0
5.18k stars 672 forks source link

Dependencies not being injected on NestJS with Serverless stack and AWS #390

Open dinesh-bade opened 3 years ago

dinesh-bade commented 3 years ago

We are trying to integrate the NestJs with serverless stack and AWS . We have simple Controller that has a Service as a dependency. when we make request to the controller the serviceObject that should contain the Service instance instead gives us undefined .

Below is our app.module.ts file contain

`

 import { Module } from '@nestjs/common';
 import { AppController } from './app.controller';
 import { AppService } from './app.service';
@Module({
 controllers: [AppController],
  providers: [AppService,
  ],
})
export class AppModule {}

`

Lambda.ts contain `

  import { NestFactory } from '@nestjs/core';
  import { ExpressAdapter } from '@nestjs/platform-express';
  import serverlessExpress from '@vendia/serverless-express';
  import { Context, Handler } from 'aws-lambda';
  import express from 'express';

  import { AppModule } from './app.module';

  let cachedServer: Handler;

  async function bootstrap() {
    if (!cachedServer) {
      const expressApp = express();
      const nestApp = await NestFactory.create(
        AppModule,
        new ExpressAdapter(expressApp),
      );

      nestApp.enableCors();

      await nestApp.init();

      cachedServer = serverlessExpress({ app: expressApp });
    }

    return cachedServer;
  }

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

`

Main.ts

`

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  await app.listen(3000);
}

bootstrap();

`

brettstack commented 3 years ago

I recommend getting the example Nest application running first and then adding in your own app on top. That will help diagnose the problem.

dinesh-bade commented 3 years ago

@brettstack it works completely fine if i just run it with nest js start command and but while i try run it from serverless-stack it's not working . We are getting below warning `

  node_modules/express/lib/view.js:81:13: warning: This call to "require" will not be bundled because the argument is not a string literal (surround with a try/catch to silence this warning)
      81 │     var fn = require(mod).__express
         ╵              ~~~~~~~

`

jclab-joseph commented 3 years ago

Did you find a way? I am having the same problem.

jclab-joseph commented 3 years ago

I found it.

const app = await NestFactory.create(AppModule);
await app.init();

If you don't call listen, you have to explicitly call init.

juventus18 commented 2 years ago

Having the same issue when using as a "standalone app" running locally with sam local invoke: https://docs.nestjs.com/standalone-applications

webmozart commented 1 week ago

I'm currently experiencing the same problem, and I do call await nestApp.init() as in the example of the original poster. Any other solution proposals?