0

I have a node.js api and have a requirement to have a config file that is fetched every 5 minutes. This config file is used throughout the application. I’ve tried the below, but the value always results in the initial value

var config = {};
refreshConfig => () = {
   try {
      let response = fetch(some network call);
      config = response;
   } catch e {
      console.log(e);
   }
}

setInterval(refreshConfig, 5*60*1000);

module.exports = config;

The scheduled process runs, but if I require the variable in any other file, the value is always {}

Football52
  • 123
  • 3
  • 14
  • May be this explains: [Is there a way to "require" a JS file only once in nodejs?](https://stackoverflow.com/questions/8958097/is-there-a-way-to-require-a-js-file-only-once-in-nodejs) – prasad_ Jun 01 '23 at 02:55
  • Reassigning `config` changes the object reference of `config`, but `module.exports` is still referencing the old object (as `module.exports` is only assigned once). You should modify the content of config (e.g. by `Object.assign`), instead of reassigning `config`. – Ricky Mo Jun 01 '23 at 03:59

0 Answers0