2

I have recently been working on an Electron application which requires the storage of data in a javascript file which gets decrypted when the user logs in and displayed, and encrypted when the user logs out. However, while logged in, the user has the option to add data to the javascript file. Unfortunately though, when this process is complete, the new data is not displayed, despite using the exact same code in the initial display as in the reload. I am confident that this is due to the javascript file needing to be reloaded (file changes are not registered by Electron). I have tried the electron-reload module, but it seems to only allow for live reloads. I need a module or solution which allows me to do something like this.

var reload = require('some-reload-module');
reload.reload('../path/to/file.js');
...

Any solutions would be welcome as I have so far had no luck. Thank you in advance!

arctic_hen7
  • 1,344
  • 11
  • 13

1 Answers1

0

This is happening due to the fact that require caches its results in require.cache. To get around this, you can just delete the entry in the cache.

// Initially require the file; the result is cached.
require('../path/to/file.js');

// Delete the cached version of the module.
delete require.cache[require.resolve('../path/to/file.js')];

// Re-require the file; the file is re-executed and the new result is cached.
require('../path/to/file.js');
Clavin
  • 1,182
  • 8
  • 17