0

I am attempting to create a script that has the ability to remove modules via the module decache.

After a lot of research i have figured out that this is possible, everyone describes it as using a different require syntax than normal.

Unfortunately i have tried all of the different methods but nothing seems to be able to decache the module.

Here is the code.

    \\server.js
    var plugins = require(__dirname + '\\plugins\\catelog.js');

    console.log('server started');
    plugins.magic();
    plugins.decache('./log/log.js');
    plugins.magic();

Here is the manifest of all of the requires that my project needs.

   \\plugins\catelog.js
     var nfo = {
      fname: __dirname + '//catelog.js',
      cname: 'Catelog',
      wd: __dirname,
      purpose: 'list of sources for require statement.',
      exuse: "require(wd + '\\httpd.js');"
    };
    console.log(nfo.cname + ' Plugin Mounted Sucsesfully');

    //indecahable entities.
    //essential packages.
    var decache = require('decache');


    //decachable entities.
    //your custom code
    var httpd = require('./httpd/httpd.js');
    var logd = require('./log/log.js');





    module.exports = {
      catelog: nfo,
      httpd: httpd,
      log: logd,
      magic: logd.magic,
      logd: logd.logd,
      decache: decache
    };

Here is the example log module i am trying to decache.

\\plugins\log\log.js
var nfo = {
  fname: './log/log.js',
  wd: __dirname,
  cname: 'Logging',
  purpose: 'log server for the main server.',
  exuse: "function(logfile,logtext,err);"
};
var fs = require('fs');
console.log(nfo.cname + ' Plugin Mounted Sucsesfully');
function logd(logfile,logtext,err){
fs.appendFile(__dirname + "../../" + logfile, '\r\n' + logtext, function (err) {
if (err){
 console.log(err);
}
});

};

function magic(text){
console.log('text');
};

module.exports = {
 nfo: nfo,
 logd: logd,
 magic: magic
};

When the code is ran, this is the output.

enter image description here

Unfortunately it appears that nothing happens, the module isnt unloaded and the function from the module can still be called upon before and after the decaching.

Does anyone have any ideas as to why this may be happening?

Christopher Allen
  • 7,769
  • 5
  • 20
  • 35
  • Try wrapping your last plugins.magic(); in a timeout. My guess is this is executing before the decache completes.... – Gary Dec 01 '15 at 23:13
  • After using a timeout on 9000 ms, the script was still able to call the function. – Christopher Allen Dec 01 '15 at 23:40
  • In looking at the code for decache, it does not look like it actually destroys the object, just the cache. You may have to just set logd = null. Look at http://stackoverflow.com/questions/15666144/how-to-remove-module-after-require-in-node-js also – Gary Dec 02 '15 at 01:11

0 Answers0