0

Newbie question....

I have a locally working node.js application which I am now trying to deploy express to AWS lambda. I have used this guide to deploy a test version (which worked).

I now am trying to implement my application which uses ES6 (and has type: module in package.json).

In my application I have added

import serverless from 'serverless-http'

but I cannot figure out the appropriate syntax for the export - the original was...

module.exports.handler = serverless(app);

I have tried:


const handler = async (app) =\> {
return serverless(app)
}

export default handler

Error message received:

2022-11-05T15:50:25.962Z undefined ERROR Uncaught Exception

"errorType": "Runtime.HandlerNotFound",
"errorMessage": "app.handler is undefined or not exported",

"stack": [
    "Runtime.HandlerNotFound: app.handler is undefined or not exported",
    "    at Object.UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:979:15)",
    "    at async start (file:///var/runtime/index.mjs:1137:23)",
    "    at async file:///var/runtime/index.mjs:1143:1"
]

I strongly suspect I am missing some fundamental understanding - truly appreciate some guidance.

Paul K
  • 1
  • 1
  • And the error is? – derpirscher Nov 05 '22 at 15:36
  • Hi @derpirscher "errorType": "Runtime.HandlerNotFound", "errorMessage": "app.handler is undefined or not exported", "stack": [ "Runtime.HandlerNotFound: app.handler is undefined or not exported", " at Object.UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:979:15)", " at async start (file:///var/runtime/index.mjs:1137:23)", " at async file:///var/runtime/index.mjs:1143:1" ] } – Paul K Nov 05 '22 at 15:51
  • Does this answer your question? [\`export const\` vs. \`export default\` in ES6](https://stackoverflow.com/questions/33611812/export-const-vs-export-default-in-es6) – derpirscher Nov 05 '22 at 16:08
  • Hi @derpirscher, thank you for suggesting this. I cannot see here what syntax I would use to make the export handler work. – Paul K Nov 05 '22 at 16:16

1 Answers1

-1

The reason for the error is that you are sending a default export when AWS Lambda is expecting a named export.

The issue is the same as with all ES6 imports/exports:

// export.js
export default const defaultExport = "foo"
export const namedExport = "bar"

// import.js
import { defaultExport } from "./export.js" // error, cannot find defaultExport
import { namedExport } from "./export.js" // success, found namedExport
import defaultExport from "./export.js" // success, found defaultExport

So, it's like the case above where you're sending the defaultExport, but AWS Lambda wants the { namedExport }. You just need to remove default from your export, and make sure you're building the handler properly. Here is a suggestion to do that:

const lambda = serverless(app)

export async function handler(event, context) {
  return lambda(event, context)
}

I've tested and it's working with serverless-offline using Node18.x. You can read more about exports on MDN.

offscriptdev
  • 122
  • 8