serverless-express icon indicating copy to clipboard operation
serverless-express copied to clipboard

Dependencies not being injected on NestJS with Serverless stack and AWS

Open dinesh-bade opened this issue 4 years ago • 9 comments

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();

`

dinesh-bade avatar Mar 17 '21 08:03 dinesh-bade

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

brettstack avatar Mar 17 '21 09:03 brettstack

@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
         ╵              ~~~~~~~

`

dinesh-bade avatar Mar 17 '21 11:03 dinesh-bade

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

jclab-joseph avatar Oct 27 '21 08:10 jclab-joseph

I found it.

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

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

jclab-joseph avatar Oct 27 '21 09:10 jclab-joseph

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

juventus18 avatar Jan 20 '22 19:01 juventus18