You could use dotenv to load configuration variables easily from a .env file. It's very convenient for database urls and api keys and similar things.
If you want to keep your solution to read the config file, though, there are a couple of things you can do about the pending promise:
The easiest is to load the file with fs.readFileSync. If you are loading the file only once at start up, there is no reason why you need that call to be async.
If you really need a start up operation that is asynchronous (like connecting to a MongoDB instance), you can hook your start up function at the point where the promise resolves and inject the resolved value to all the code that is dependant on it.
conststartApp=conf=>{// Do everything that you need to start your app here.}constconf=awaitch.loadConf()startApp(conf)
I don't use async/await, but you can take a look at how I start an express server after initiating a connection to MongoDB here.
To reiterate though, I strongly suggest you use dotenv to store database urls and similar data that you don't want to commit to your version control system. And if that doesn't suit you for any reason, there is likely no good reason not to use fs.readFileSync to read your config file at the app start.
You could use dotenv to load configuration variables easily from a
.env
file. It's very convenient for database urls and api keys and similar things.If you want to keep your solution to read the config file, though, there are a couple of things you can do about the pending promise:
The easiest is to load the file with
fs.readFileSync
. If you are loading the file only once at start up, there is no reason why you need that call to be async.If you really need a start up operation that is asynchronous (like connecting to a MongoDB instance), you can hook your start up function at the point where the promise resolves and inject the resolved value to all the code that is dependant on it.
I don't use
async/await
, but you can take a look at how I start an express server after initiating a connection to MongoDB here.To reiterate though, I strongly suggest you use dotenv to store database urls and similar data that you don't want to commit to your version control system. And if that doesn't suit you for any reason, there is likely no good reason not to use
fs.readFileSync
to read your config file at the app start.dotenv
looks like it does exactly what I need, thank you so much 🙂