serverless: module initialization error: Error
This is a Bug Report
Description
Anytime the Lambda function is run i get:
module initialization error: Error
regardless of whether I curl them or run it straight from Lambda Management Console using the Test button. The functions work fine when I run them locally using sls invoke local -f
Source Code
All my Lambda functions from this service fail with this same error. I even uploaded a very simple function with no dependencies that still fails. From handler.js:
module.exports.healthcheck = (event, context, callback) => {
console.log(event);
callback(null, {
statusCode: 200,
body: JSON.stringify({
message: `All good: ${new Date().toTimeString()}.`,
env: `${process.env.NODE_ENV}`,
}),
});
};
The relevant section from the serverless.yaml is
functions:
healthcheck:
handler: handler.healthcheck
events:
- http:
path: healthcheck
method: get
cors: true
Additional Data
- Serverless Framework Version you’re using: 1.6.1
- Operating System: macOS Sierra 10.12.2 (local) & Ubuntu 14.04 (CircleCI / deploy)
- Stack Trace:
Straight from CloudWatch, below is all there is

Any assistance would be greatly appreciated!
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 7
- Comments: 27 (4 by maintainers)
For me this was down to using the
bcryptmodule. The version in my node_modules directory was compiled on macos and didn’t work on Amazon Linux. After reading https://github.com/kelektiv/node.bcrypt.js/issues/505 I switched to bcryptjs and that fixed it.@daviskoh can you sure AWS support response? Seems like we’ve just started to get this also
Yes, I had forgotten to provide an environment variable: https://stackoverflow.com/a/50193095/1176596. I used
git bisectto find out the commit that introduced the problem which lead me into the right direction 🙂Has there been a solid solution found to this error? Currently running into this issue and haven’t came across a solution yet. 😩
For me, it turned out that there were special
NULcharacters of some sort in my code, that came from copying text from a Windows machine I had VPN’ed into for a DB IDE. They were at the end of each line / word I had copied. I wasn’t seeing them in PyCharm, but Sublime Text showed me. Getting rid of them removed the error.This indeed seems to have been an issue w/ AWS Lambda. Nothing was wrong with my setup as the issue has “resolved” itself and the functions are running like before even though I haven’t pushed up any changes since yesterday afternoon. I will open an issue w/ AWS Tech Support as likely has something to do w/ the Lambda environment.
Closing this.
Sadly, it appears that this error has multiple solutions, and not incredibly detailed error handling. I’m having the issue myself, haven’t yet figured out why - considering that I have an almost identical set of lambda’s deployed, and they are working just fine. Will take a look through some of these suggestions!
Update: running in sls local gave me more helpful debug information, turns out I was declaring a mongoose Boolean type but I was spelling it as BOOLEAN. Guess I was in Postgres mode.
Hey @daviskoh thanks for opening this issue.
It seems like AWS Lambda is not quite sure how to resolve your handler. Could you share your directory structure?
Additionally you might want to look into the
Lambdasection in your AWS console. Their you might see some more information about what’s wrong with your function setup.I have the same error and I solved it by adding --skipCacheInvalidation parameter. It is my start command:
NODE_ENV=development nodemon --exec \"serverless offline start\" -e \"extensions,to,watch,for,example,js,elm,hs,py\" --skipCacheInvalidationFor me, it was
source-map-supportthat discarded the actual error message and stack trace. Once I commented out thesource-map-supportimport line then the error started showing the actual error message and stack trace. https://github.com/evanw/node-source-map-support/issues/240I was just having this issue as well. The comment from @marktani made me think it may have had something to do with the ENV variables. I had an unnecessary present in my
.babelrcforenv. I stripped it out and now it’s workingAwesome thank you for the response!