[solved] How to force ``require-reload`` when using ``require``?


#1

Hi guys,

in my tests I need to RELOAD-REQUIRE a file in order for it to have the correct data. Right now I am using require() but I am getting a CACHED version (from require.cache) with wrong OUT-DATED data.

I found some NPM-modules that promise to give real reload-behaviour, tried them out, but the WON’T WORK IN METEOR context. I guess it is because the require module.export-syntax ( https://nodejs.org/api/modules.html#modules_module_exports )?
https://www.npmjs.com/package/require-uncached
https://www.npmjs.com/package/require-reload

What is the METEOR-WAY of require-reload a file WITHOUT using the cached version?


Application Structure
#2

Can you use Npm.require('file.js')

btw another way requires https://atmospherejs.com/meteorhacks/npm e.g

var GithubApi = Meteor.npmRequire('github');
      var github = new GithubApi({
          version: "3.0.0"
      });

#3

@tomtom87: Thanks for the quick respone!! are you sure that this will NOT load a cached file when being called multiple times?

Meanwhile I have found a solution. It was hidden in Nodes docs https://nodejs.org/api/modules.html#modules_module_require_id

If you want to have a module execute code multiple times, then export a
function, and call that function

So this is exactly the way I do it now… no needs for any hacks. :slight_smile:


#4

@thebarty ahhh yeh of course with exports! There was a method in the npm package for ensuring no cache also if that route does not work mate. Best of luck with it!!


#5

One more option - delete the file from the require cache first:

...
const resolved = require.resolve(
  `${__dirname}/${imports}/api/widgets/server/widget_synch.js`
);
delete require.cache[resolved];
...