node.js - node require.cache delete does not result in reload of module -
i'm writing tests npm module. these tests require install multiple versions of npm module in order check if module validate them compatible or incompatible.
somehow uncache libraries or function found on stackoverflow or npm database not working..
i install/uninstall npm modules using helper functions:
function _run_cmd(cmd, args) { return new promise((res, rej) => { const child = spawn(cmd, args) let resp = '' child.stdout.on('data', function (buffer) { resp += buffer.tostring() }) child.stdout.on('end', function() { res(resp) }) child.stdout.on('error', (err) => rej(err)) }) } global.helper = { npm: { install: function (module) { return _run_cmd('npm', ['install', module]) }, uninstall: function (module) { decachemodule(module) return _run_cmd('npm', ['uninstall', module]) } } }
this current decache function should clear modules caches (i tried others, including npm modules none of them worked)
function decachemodules() { object.keys(require.cache).foreach(function(key) { delete require.cache[key] }) }
i installing multiple versions of less module (https://www.npmjs.com/package/less)
in first test installing deprecated version not have render
-function.
in other test installing up-to-date version has render
-function. somehow if test function test fail. if skip first test other test succeeds. (render
-function exists).
this makes me believe deletion of require.cache has no impact...
i using node v4.2.4.
if there reference old module:
var less = require('less');
clear module cache not lead reference cleared , reload module. work, @ least don't store module variable, use "require('less')" in place everywhere.
Comments
Post a Comment