"invalid ELF header" when using the nodejs "ref" module on AWS Lambda

For those Developers porting JS to TypeScript on Lambda, ensure ALL dependancies are imported, not required:

import $ from "cheerio";

NOT

const $ = require("cheerio");

This happens when you build on one architecture and then attempt to use the same built addon on a different architecture (or platform in some cases).

According to this page, addons may have to be compiled against an Amazon Linux image on EC2 in order for the compiled addons to be usable on their Lambda service.


This doesn't apply exactly to your question, but in case anyone stumbles across this when using AWS Elastic Beanstalk, it could be a result of uploading the node_modules/ directory along with everything else, so you need to force it to ignore the file.

.ebignore

node_modules/