sodium-friends / sodium-native

Low level bindings for libsodium
MIT License
300 stars 64 forks source link

No native build was found - EsBuild AWS Lambda #184

Closed rdzidziguri closed 5 months ago

rdzidziguri commented 1 year ago

I know this uses native modules and probably needs to be treated differently; however, I do not have much experience with native modules. I can easily handle layers, but the use case and problem I am facing is that whenever I try to use the EsBuild bundler and either run it locally on Windows 11 or AWS Lambda, I get the following No native build was found for platform error. could someone advise or even confirm if running the package on AWS Lambda is feasible at all? environment is Node18

cakoose commented 11 months ago

The problem:

  1. Normally, sodium-native looks for the native library binary in a location relative to sodium-native's "index.js" file by using the __dirname magic variable.
  2. When ESBuild bundles things, it inlines sodium-native's "index.js" into the bundle.
  3. When the bundle runs, __dirname is the folder of the bundle rather than the folder of sodium-native's "index.js", so it can't find the native library binary.

The hack I'm currently using: add --external:sodium-native to the ESBuild command line.

The downside of that hack is that sodium-native's JS code is no longer part of the ESBuild bundle. It will be require'd at runtime, so the bundle is no longer self-contained. You need to include the sodium-native library with your deployment, e.g. in "node_modules".

The ESBuild issue tracker has some examples of how to use an ESBuild plugin to fake out the __dirname magic variable to refer to the location of the original file: https://github.com/evanw/esbuild/issues/859. I haven't tried it out.

mixmix commented 11 months ago

I have used https://github.com/martonlederer/esbuild-plugin-fileloc to get a build working locally (one of the solutions mentioned in that thread).

Have not tried in lambda