node require.cache delete does not result in reload of module
<p>I'm writing tests for my npm module.
These tests require to install multiple versions of an npm module in order to check if the module will validate them as compatible or incompatible.</p>

<p>Somehow all uncache libraries or function I found on stackoverflow or the npm database are not working..</p>

<p>I install/uninstall npm modules by using my helper functions:</p>

<pre><code>function _run_cmd(cmd, args) {
    return new Promise((res, rej) =&gt; {
        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) =&gt; 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])
        }
    }
}
</code></pre>

<p>This is my current decache function which should clear all modules caches (I tried others, including npm modules none of them worked)</p>

<pre><code>function decacheModules() {
    Object.keys(require.cache).forEach(function(key) {
        delete require.cache[key]
    })
}
</code></pre>

<p>I am installing multiple versions of the less module (<a href="https://www.npmjs.com/package/less" rel="nofollow">https://www.npmjs.com/package/less</a>)</p>

<p>In my first test I am installing a deprecated version which does not have a <code>render</code>-function.</p>

<p>In some other test I am installing an up-to-date version which has the <code>render</code>-function. Somehow if I test for that function that test does fail.
If I skip the first test the other test succeeds. (<code>render</code>-function exists).</p>

<p>This makes me believe that the deletion of require.cache has no impact...</p>

<p>I am using node v4.2.4.</p>