Closed mstaicu closed 4 months ago
It seems that this is not working because of how ES Modules are resolved in node.js
Guess what I'm trying to find out is, can I initialize nconf
once and then use it in all ESM files without redoing the .env.argv.file
?
Figured it out, I used dynamic ESM imports, now this works
import nconf from "nconf";
nconf.env({ lowerCase: true, parseValues: true, separator: "_" });
import('./server.js');
Glad you figured it out! Thanks for posting your solution. I'm sure it'll be helpful to others as well.
Ended up with this file structure:
src
├── app.js
├── index.js
├── server.js
// server.js
import nconf from "nconf";
import { app } from "./app.js";
var port = nconf.get("AUTH_EXPRESS_PORT");
app.listen(port, () => console.log(`Listening on port ${port}`));
// index.js
import nconf from "nconf";
nconf.env();
import("./server.js");
Running the web app server via AUTH_EXPRESS_PORT=3000 node src/index.js
, using that variable only for the current shell, or you can set it for current shell and all processes started from current shell by first doing export AUTH_EXPRESS_PORT=3000
and then running node src/index.js
I have the following example
This works, I get the correct value in
nconf.get("express:port")
, but if I do the same inapp.js
I getundefined
. I tried this as wellAnd I get
undefined
inserver.js
when runningnconf.get("express:port")