2

I have a configuration JSON module config.json like:

{
    "comment": "This is old config"
}

I'm using require('./config.json') to import it as a module. Some where in my source code, I want to update the content of JSON file and reload it like new content:

{
    "comment": "This is new config"
}

For example, in index.js I will rewrite config.json file and reimport it like below:

const fs = require("fs");
const path = require("path");
let config = require("./config.json");

console.log('Old: ' + config.comment);

// Rewrite config
let newConfig = {
  comment: "This is new config"
};

fs.writeFileSync(
  path.join(process.cwd(), "config.json"),
  JSON.stringify(newConfig, null, "\t")
);

// Reload config here
config = require("./config.json");
console.log('New: ' + config.comment);

Console's output:

Old: This is old config
New: This is old config

I saw JSON content updated, but I cannot reload module, config variable still contains the same cache data before. How can I rewrite and reimport JSON file as module?

Any suggestion is appreciated.

ThanhPhanLe
  • 1,315
  • 3
  • 14
  • 25
  • 1
    Possible duplicate of [Node.js: how to reload module](https://stackoverflow.com/questions/33546880/node-js-how-to-reload-module) – ambianBeing Aug 19 '19 at 10:49
  • @ambianBeing but your reference does not provide an answer? – ThanhPhanLe Aug 19 '19 at 10:52
  • Try to use `require('./config.json')` after you update `config.json` – Omri Attiya Aug 19 '19 at 10:52
  • So your using a json file as data and when that file changes you want to re-read that data? Or when you rebuild you want to reload the module? `require`(ing) it in is the wrong thing to do if it's data. That designed to load source code, it won't change unless you rerun your source. – Liam Aug 19 '19 at 10:53
  • @OmriAttiya I did it, but it does not work – ThanhPhanLe Aug 19 '19 at 10:54
  • @Liam I want to reload it as module, not a data, because I want to use it as my config module and access it from somewhere – ThanhPhanLe Aug 19 '19 at 10:55
  • Try what's written in the link @ambianBeing added. You need to clear the cache of this module, then you can reload it – Omri Attiya Aug 19 '19 at 10:55
  • Possible duplicate of [node.js require() cache - possible to invalidate?](https://stackoverflow.com/questions/9210542/node-js-require-cache-possible-to-invalidate) – Liam Aug 19 '19 at 10:55
  • @ThanhPhan Please can you try this `delete require.cache[require.resolve('./config.json')];` I only marked it because the post contains some discussion about safely deleting require cache. Also [link](https://stackoverflow.com/a/14801711/6082280) – ambianBeing Aug 19 '19 at 10:56
  • @ambianBeing `delete require.cache[require.resolve("./config.json")];` yeah it works, thank you. But I have to provide an string `"./config.json"` as a module name, why I cannot delete cache by object like `delete require.cache[require.resolve(config)];` – ThanhPhanLe Aug 19 '19 at 11:00
  • 2
    @ThanhPhan Because AFAIK module resolution in node happens via dependency path (relative/absolute) and under the hood `require` uses `require.resolve()` where the `cache` dictionary generated contains `key` as the `path`. And that is why we have to resolve and delete via path. Please (anybody) do correct me if that assertion is wrong. – ambianBeing Aug 19 '19 at 11:09

1 Answers1

4
const fs = require("fs");
const path = require("path");
let config = require("./config.json");

console.log('Old: ' + config.comment);

// Rewrite config
let newConfig = {
  comment: "This is new config"
};

fs.writeFileSync(
  path.join(process.cwd(), "config.json"),
  JSON.stringify(newConfig, null, "\t")
);

// Reload config here
delete require.cache[require.resolve('./config.json')]   // Deleting loaded module
config = require("./config.json");
console.log('New: ' + config.comment);

Just added a line to remove the preloaded module, before reloading it.

nim118
  • 142
  • 1
  • 9