libxmljs2 icon indicating copy to clipboard operation
libxmljs2 copied to clipboard

AWS lambda webpack bundling issue - “Could not find module root given file: \”/var/task/index.js\"

Open raffihsieh opened this issue 2 years ago • 1 comments

Originally posted here: https://github.com/cdegalitt/libxmljs2-xsd/issues/7 I am running in to the same issue using only this libxmljs2 package, so I'm opening up an issue here for visibility.

Leigh-M commented on Apr 6 • I have an AWS lambda function that runs fine until I include this lib - then throws with full error:

    "errorMessage": "Could not find module root given file: \"/var/task/index.js\". Do you have a `package.json` file? ",
    "stack": [
        "Error: Could not find module root given file: \"/var/task/index.js\". Do you have a `package.json` file? ",
        "    at Function.getRoot (/var/task/index.js:5286:13)",
        "    at bindings (/var/task/index.js:5157:32)",
        "    at Object../node_modules/libxmljs2/lib/bindings.js (/var/task/index.js:7094:92)",
        "    at __webpack_require__ (/var/task/index.js:16311:42)",
        "    at Object../node_modules/libxmljs2/index.js (/var/task/index.js:7042:18)",
        "    at __webpack_require__ (/var/task/index.js:16311:42)",
        "    at Object../node_modules/libxmljs2-xsd/index.js (/var/task/index.js:6954:16)",
        "    at __webpack_require__ (/var/task/index.js:16311:42)",
        "    at Object../src/validateXML.js (/var/task/index.js:16099:13)",
        "    at __webpack_require__ (/var/task/index.js:16311:42)"

It is being bundled via webpack so possible it is bundling that is the issue - although no other modules appear to have this issue. It looks like it is calling libxmljs2 and not able to resolve from the package.json. Any thoughts much appreciated. Thanks in advance

Just an addendum to this, I actually managed to get this running (this is not clean and it still needs resolving for anyone else running into this). It appears that it needs any package.json just containing an empty {} is fine, in the /dist folder and 2 binaries from node modules to run, in full:

My webpacked /dist folder now looks like:

/dist
   /someHandler
      index.js
   /anotherHandler
      /build
         node-libxml-xsd.node
         xmljs.node
      index.js
      package.json

the node-libxml-xsd.node binary was in node_modules/libxmljs2-xsd/build and xmljs.node was in node_modules/build/Release

So it does now run with the work-around solution, but I am not certain why this module is not being bundled correctly by webpack - my lack of knowledge around node_modules & webpack bundling here. Thanks #

raffihsieh avatar Oct 13 '21 17:10 raffihsieh

after trying out the hack solution above, my lambda still gives me an error:

2021-10-14T11:40:10.360-07:00

Copy
2021-10-14T18:40:10.359Z	undefined	ERROR	Uncaught Exception 	
{
    "errorType": "Error",
    "errorMessage": "/var/task/build/xmljs.node: invalid ELF header",
    "stack": [
        "Error: /var/task/build/xmljs.node: invalid ELF header",
        "    at Object.Module._extensions..node (internal/modules/cjs/loader.js:1131:18)",
        "    at Module.load (internal/modules/cjs/loader.js:937:32)",
        "    at Function.Module._load (internal/modules/cjs/loader.js:778:12)",
        "    at Module.require (internal/modules/cjs/loader.js:961:19)",
        "    at require (internal/modules/cjs/helpers.js:92:18)",
        "    at bindings (/var/task/lambda.js:3616:48)",
        "    at Object../node_modules/libxmljs2/lib/bindings.js (/var/task/lambda.js:82405:92)",
        "    at __webpack_require__ (/var/task/lambda.js:202540:42)",
        "    at Object../node_modules/libxmljs2/index.js (/var/task/lambda.js:82353:18)",
        "    at __webpack_require__ (/var/task/lambda.js:202540:42)"
    ]
}

a quick search brings us here: https://aws.amazon.com/blogs/compute/nodejs-packages-in-lambda/ which pretty much says that native libraries need to be compiled on the amazon linux EC2 image to work properly.

raffihsieh avatar Oct 14 '21 18:10 raffihsieh