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.
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?