jsonpath icon indicating copy to clipboard operation
jsonpath copied to clipboard

cannot bundle with CDK/esbuild and use in AWS Lambda

Open pveller opened this issue 3 years ago • 7 comments

Hey guys,

awesome library. Love it! But I can't use it in lambda packaged with esbuild without some hacking because of this:

https://github.com/dchester/jsonpath/blob/master/lib/grammar.js#L102

It comes down to this in runtime:

{
    "errorType": "Runtime.ImportModuleError",
    "errorMessage": "Error: Cannot find module '../include/module.js'\nRequire stack:\n- /var/task/index.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
    "stack": [
        "Runtime.ImportModuleError: Error: Cannot find module '../include/module.js'",
        "Require stack:",
        "- /var/task/index.js",
        "- /var/runtime/UserFunction.js",
        "- /var/runtime/index.js",
        "    at _loadUserApp (/var/runtime/UserFunction.js:202:13)",
        "    at Object.module.exports.load (/var/runtime/UserFunction.js:242:17)",
        "    at Object.<anonymous> (/var/runtime/index.js:43:30)",
        "    at Module._compile (internal/modules/cjs/loader.js:1085:14)",
        "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)",
        "    at Module.load (internal/modules/cjs/loader.js:950:32)",
        "    at Function.Module._load (internal/modules/cjs/loader.js:790:12)",
        "    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12)",
        "    at internal/main/run_main_module.js:17:47"
    ]
}

There are no files other than nicely bundled index.js after esbuild

Thanks!

pveller avatar Mar 31 '22 18:03 pveller

Same problem here. Any quick fixes for this?

askluke avatar Apr 12 '22 15:04 askluke

Was a fix for this issue found?

It seems like esbuild is shaking off the include folder. That being said, can you guys create a repro project with esbuild and the input that is creating this problem? I can look in to it and attempt a fix.

EarthyOrange avatar Sep 23 '22 17:09 EarthyOrange

I just switched to https://github.com/JSONPath-Plus/JSONPath

pveller avatar Sep 30 '22 13:09 pveller

Hi ! It's shaked off because both files in the missing "include" folder are required dynamically (no static require / import from) : https://github.com/dchester/jsonpath/blob/master/lib/grammar.js#L102

if (fs.readFileSync) {
  grammar.moduleInclude = fs.readFileSync(require.resolve("../include/module.js"));
  grammar.actionInclude = fs.readFileSync(require.resolve("../include/action.js"));
}

This syntax forbids all usage of this library with a bundler (esbuild, webpack, nx, ...) that uses tree shaking...

xgpayfit avatar Jan 16 '23 22:01 xgpayfit

this dynamic import needs to be fixed it breaks a lot of things

brianorwhatever avatar Jun 14 '23 16:06 brianorwhatever