Error: Serverless-offline: handler for 'HelloWorldLambda' is not a function

My setup

Typescript + webpack, the part of the setup what works.

serverless.yml

service: hello-service
plugins:
- serverless-webpack
- serverless-offline
functions:
  HelloWorldLambda:
    handler: HelloService.add
      - 
        http:
          path: /hello/add
          method: get

HelloService.ts

import Service from "..."
import Hello from "..."

class HelloService extens Service<Hello> {
  async add(event: APIGatewayEvent, context: Context, cb: Callback): Promise<ApiGatewayResponse> {
    const entity = Service.getRequestedEntityProps(event);
    try {
       const result = await this.repository.addOrUpdate(entity);
       return Response.add(result.value());
    } catch (e) {
       return Response.fail(e);
    }
  }
  /* implementation of update, remove, etc... */
}

function getHandlers(service: Service<Hello>) {
  return {
    add: service.add,
    update: service.update,
    remove: service.remove,
  };
}

export default getHandlers(new HelloService());

Full stacktrace

Serverless: GET /hello/add (λ: HelloWorldLambda)
Serverless: Error while loading HelloWorldLambda
Error: Serverless-offline: handler for 'HelloWorldLambda' is not a function
    at Object.createHandler (./node_modules/serverless-offline/src/functionHelper.js:221:11)
    at handler (./node_modules/serverless-offline/src/ApiGateway.js:485:40)
    at module.exports.internals.Manager.execute (./node_modules/@hapi/hapi/lib/toolkit.js:41:33)
    at Object.internals.handler (./node_modules/@hapi/hapi/lib/handler.js:46:48)
    at exports.execute (./node_modules/@hapi/hapi/lib/handler.js:31:36)
    at Request._lifecycle (./node_modules/@hapi/hapi/lib/request.js:312:68)
    at <anonymous>
    at process._tickDomainCallback (internal/process/next_tick.js:228:7)

Steps to reproduce

  1. Copy the setup.
  2. Run serverless offline start on the command line.
  3. Go to the url http://localhost:3000/hello to run the function.

Expected result

The getHandlers function should return an object thus the HelloService will do a export default of an object with the keys add, update and remove. The value of a object property should be method of the service, what will work similar as a plain function.

1 Like

Anyone got any idea, why it is not working. When I console log the getHandlers(new HelloService()) it shows me the following output, when calling the route:

{ add: [Function],
  update: [Function],
  remove: [Function] }

Isn’t that a bit weird, the error message says it’s not a function however the console.log() does!?

did you find the solution to this problem?