Search code examples
node.jslambdaaws-lambdaweb-workerserverless-framework

AWS Lambda fails with NodeJS Dependencies


I'm receiving the following error when executing my NodeJS Lambda function with dependencies. Running NodeJS v8.4.0 with NPM v5.6.0 on MacOSX High Sierra v10.13.3 Beta.

"errorMessage": "/var/task/node_modules/webworker-threads/build/Release/WebWorkerThreads.node: invalid ELF header",
"errorType": "Error",
"stackTrace": [
    "Object.Module._extensions..node (module.js:597:18)",
    "Module.load (module.js:487:32)",
    "tryModuleLoad (module.js:446:12)",
    "Function.Module._load (module.js:438:3)",
    "Module.require (module.js:497:17)",
    "require (internal/module.js:20:19)",
    "bindings (/var/task/node_modules/bindings/bindings.js:81:44)",
    "Object.<anonymous> (/var/task/node_modules/webworker-threads/index.js:1:105)",
    "Module._compile (module.js:570:32)"
]

I've narrowed it down to the "natural" module which requires the WebWorkerThreads module triggering the error. Below is my dependencies.

"dependencies": {
    "natural": "^0.5.6",
    "path": "^0.12.7"
}

I've raised this question on AWS Forums with no answers, and would greatly appreciate any help the stack overflow community could give. I've done allot of googling and found the following answers. I'm not using custom modules or Docker though.


Solution

  • Some npm modules use native binaries that are being compiled when you do npm install. These compiled binaries only run on the OS/platform where they were compiled.

    Because of the above, those native dependencies that you compiled on your MacOS will NOT work once you upload them on AWS Lambda since Lambda runs on Linux.

    To solve your problem, you would need to create your Lambda deployment package (including npm install) on a Linux machine. You have several ways to do this:

    1. Use a Linux Virtual Machine (e.g. Virtualbox or Parallels) and do your npm install from inside that VM.

    2. Use vagrant. Same as number 1.

    3. Use docker. Still very similar to number 1 and 2.

    Or, simply use pure JS dependencies and you won't have the above problem in the first place. Many native npm modules now have pure JS alternatives.