Search code examples
javascriptnode.jsgithubwebhooksprobot

Refactoring probot event functions into seperate file causes error: TypeError: handler is not a function


I have the vanilla probot event function from the docs that comments on new issues:

const probotApp = app => {
  app.on("issues.opened", async context => {
    const params = context.issue({ body: "Hello World!" });
    return context.github.issues.createComment(params);
  });
}

This works fine.

I refactor the code into a separate file:

index.js

const { createComment } = require("./src/event/probot.event");

const probotApp = app => {
  app.on("issues.opened", createComment);
}

probot.event.js

module.exports.createComment = async context => {
  const params = context.issue({ body: "Hello World!" });
  return context.github.issues.createComment(params);
};

But I receive this error:

ERROR (event): handler is not a function
    TypeError: handler is not a function
        at C:\Users\User\probot\node_modules\@octokit\webhooks\dist-node\index.js:103:14
        at processTicksAndRejections (internal/process/task_queues.js:97:5)
        at async Promise.all (index 0)

When I create a test as recommended in the docs with a fixture and mock the event webhook call with nock this works fine. But when I create a real issue on GitHub this error is thrown.

How can I refactor the code into a separate file without causing the error?


Solution

  • This was my mistake.

    This is the whole probot.event.js file:

    module.exports.createComment = async context => {
      const params = context.issue({ body: "Hello World!" });
      return context.github.issues.createComment(params);
    };
    
    
    module.exports = app => {
      // some other event definitions
    }
    

    By defining module.exports = app I overwrote the previous module.export. The createComment function was therefore never exported.

    Removing module.exports = app = { ... } fixed it!