I have a super basic AWS Lambda function using serverless, express, and libxmljs (which binds JavaScript to libxml):
serverless.xml:
service: xmltest
provider:
  name: aws
  runtime: nodejs10.x
  stage: dev
  region: us-east-1
functions:
  app:
    handler: index.handler
    events:
      - http: ANY /
      - http: 'ANY {proxy+}'
plugins:
  - serverless-offline
index.js:
const serverless = require('serverless-http');
const express = require('express');
const libxml = require("libxmljs");
const app = express();
app.use(express.json());
app.post('/lookup', async function (req, res) {
  res.send({result: "hello world"});
});
module.exports.handler = serverless(app);
When I run locally:
$ curl -X POST http://localhost:3000/lookup -d {"example":123}
{"result":"hello world"}
When I run on AWS:
$ curl -X POST https://REDACTED.execute-api.us-east-1.amazonaws.com/dev/lookup -d {"example":123}
{"message": "Internal server error"}
The CloudWatch logs say:
2019-06-05T12:46:43.280Z    undefined   ERROR   Uncaught Exception
{
    "errorType": "Error",
    "errorMessage": "/var/task/node_modules/libxmljs/build/Release/xmljs.node: invalid ELF header",
    "stack": [
        "Error: /var/task/node_modules/libxmljs/build/Release/xmljs.node: invalid ELF header",
        "    at Object.Module._extensions..node (internal/modules/cjs/loader.js:730:18)",
        "    at Module.load (internal/modules/cjs/loader.js:600:32)",
        "    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)",
        "    at Function.Module._load (internal/modules/cjs/loader.js:531:3)",
        "    at Module.require (internal/modules/cjs/loader.js:637:17)",
        "    at require (internal/modules/cjs/helpers.js:22:18)",
        "    at bindings (/var/task/node_modules/bindings/bindings.js:84:48)",
        "    at Object.<anonymous> (/var/task/node_modules/libxmljs/lib/bindings.js:1:99)",
        "    at Module._compile (internal/modules/cjs/loader.js:701:30)",
        "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)"
    ]
}
How can I import libxmljs on AWS?
I see some related questions like
So I’m guessing something in libxmljs is built for a different architecture (my local machine is macOS) than Amazon Linux. But I'm not sure how I can fix this?
I solved the problem by running npm install on a Linux box instead of my Mac.
In my case I set up AWS CodePipeline which runs all the build scripts on an EC2 instance. Could also have solved this using other hosted CI pipeline, a Docker container, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With