@brettstack I have deployed a simple application on lambda and on testing I get:
@brettstack module initialization error: TypeError
at _addListener (events.js:216:11)
at Server.addListener (events.js:276:10)
at new Server (_http_server.js:234:10)
at Object.exports.createServer (http.js:44:10)
at Object.createServer (/var/task/node_modules/aws-serverless-express/index.js:144:25)
at Object.
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
You can sometimes get this if you've done npm i with a version of node greater than what Lambda supports - if your run time for your Lambdas is 6.10.3 and you npm i with 8 then you'll get all kinds of funky errors.
I happened to me either because we had unaccessible environment variables (wrong naming), that were needed to initialise environment specific resources e.g. external API endpoint.
For me when this happened I was not properly exporting the app function from my separate source file.
So when aws-serverless-express called createServer it was failing causing the module initialization.
Thank God for https://github.com/dherault/serverless-offline
For providing me with an infinitely more useful stack trace thank the lambda env
export default app;
was all I needed to add to that file
Happened to me after I imported one of the amazon services and did not use it:
var sns = new AWS.SNS();
// and never called sns
Thanks @williscool for the tip. Had the same issue but changing from module.exports = { app } to exporting as default fixed it :)
Most helpful comment
You can sometimes get this if you've done npm i with a version of node greater than what Lambda supports - if your run time for your Lambdas is 6.10.3 and you npm i with 8 then you'll get all kinds of funky errors.