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.13k stars 668 forks source link

serverless_express_1.default is not a function #442

Closed alko89 closed 3 years ago

alko89 commented 3 years ago

I'm trying to update the lib to the latest version (from v3) I have to start a mongo connection and have modified the basic-starter-nestjs example and also tried sails-example, but after deploy I get serverless_express_1.default is not a function error. I'm using Typescript:

{
    "errorType": "TypeError",
    "errorMessage": "serverless_express_1.default is not a function",
    "stack": [
        "TypeError: serverless_express_1.default is not a function",
        "    at /var/task/scripts/lambda.js:26:56",
        "    at Generator.next (<anonymous>)",
        "    at fulfilled (/var/task/scripts/lambda.js:5:58)",
        "    at processTicksAndRejections (internal/process/task_queues.js:95:5)"
    ]
}

These are the examples I tried without luck:

import serverlessExpress from '@vendia/serverless-express';
import { App, Mongo } from '..';
import * as env from '../config/env';

global['mongo'] = global['mongo'] || new Mongo(env);

let cachedServer;

async function bootstrap() {
  if (!cachedServer) {
    const api = new App({
      env,
      mongo: global['mongo'],
    });

    await global['mongo'].connect();

    cachedServer = serverlessExpress({ app: api.app });
  }

  return cachedServer;
}

export const handler = async (event: any, context: any, callback: any) => {
  const server = await bootstrap();
  return server(event, context, callback);
};
import serverlessExpress from '@vendia/serverless-express';
import { App, Mongo } from '..';
import * as env from '../config/env';

global['mongo'] = global['mongo'] || new Mongo(env);

const api = new App({
  env,
  mongo: global['mongo'],
});

exports.handler = async (event, context, callback) => {
  await global['mongo'].connect();
  return serverlessExpress({ app: api.app })(event, context, callback);
};
brettstack commented 3 years ago

Can you try return server.handler(event, context)

alko89 commented 3 years ago

Didn't help, I get the same error

somjet commented 3 years ago

try import { configure as serverlessExpress } from '@vendia/serverless-express';

alko89 commented 3 years ago

Yep, this seems to work. Thanks!

Just wondering what would be the benefit of caching the server? Would it help it invoke faster?

rudyhadoux commented 1 year ago

Hi, What is Vendia ? Who pays them for this solution ?