Module.exports = handlerFunction

First time using this. Can’t seem to set it up in a way that seems natural.

// serverless.yml
functions
redirector
handler: redirector

// redirector.js
module.exports = (event, context, callback) => { …

Not sure what’s going on here but I’ve done a search for this and can’t seem to find a reason why this doesn’t work.
Not sure why i would want to namespace my functions a second time (the first being the filename.)

Hey @SeeThruHead the handler Syntax for the serverless.yml file is

filename.exportedFunctionName

So if you have a file called redirector and export a function called redirect in there you can write handler: redirector.redirect in the serverless.yml file.

So the only thing you need to do here is to do a named export and then update the handler in your serverless.yml to point to the exported handler.

sorry, maybe i wasn’t clear. the file is the function, it does not have any subfunctions

ie:
module.exports = function() {}
rather than module.exports.redirector = function(){}

i assumed i should be able to just put ‘redirector’ inside serverless.yml, because a require(’./redirector’) should be the proper function to run

You have to export a distinct handler function - you can’t use the default export, as Lambda doesn’t support it (i.e. it’s not a Serverless issue). The convention is to call it handler.

The natural way to do it is:

serverless.yml

functions:
  redirector:
    handler: redirector.handler

redirector.js

module.exports.handler = (event, context, cb) => {
  ...
}
2 Likes