Requiring a file at a "dynamic" path

interop

#1

I’m trying to load some exported module from a dynamically (runtime) determined path.

When we run our node applications locally, we’ll typically load our secrets, etc in from a file like ./config/secrets.js. When we run in a container, we’ll load the secrets in from a file, but the location is determined by the platform and provided in an env variable such as process.env.SECRETS which may have a value like /var/run/b3e0fl_secrets.

The code for that would usually look something like const secrets = require(process.env.SECRETS || './config/secrets.js').

Is something like this possible with ReasonML/Bucklescript?


#2

Not directly, but you can always insert that line as a raw JS snippet in a module, then bind to it from the Reason code.


#3

Yeah, that’s the approach I’ve taken so far, but every time I look at it I think “there’s got to be a better way to do this.”