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

did you find the solution to this problem?

I’m having the same issue and I’m also using the serverless-webpack plugin.

serverless.yml:

provider:
  name: aws
  runtime: nodejs12.x

package:
  individually: true
plugins:
  - serverless-webpack
  - serverless-deployment-bucket
  - serverless-offline

functions:
  - ${file(./resources/consumers.yml)}

./resources/consumers.yml

my-lambda:
  handler: handlers/domain/consumers/my-lambda.handler
  events:
    - http:
        path: v1/users
        method: POST
        cors: true
    - sqs:
        batchSize: 1
        arn:
          Fn::GetAtt:
            - MYQueue
            - Arn

handlers/domain/consumers/my-lambda.ts

export const handler: Handler = async (event: SQSEvent, context: Context) => {
  try {
    await processSQSRecords(Records)
  } catch (e) {
    console.log(e)
  }
}