Browserify: fresh reload of a module

Created on 23 Nov 2015  路  5Comments  路  Source: browserify/browserify

Can I reload module to get a fresh copy of a particular module? With node, I can delete require.cache and get a new copy. Can I do something similar with browserify?

Most helpful comment

Okay so i know this is not the way its ment to be done and i really think browserify should add some sort of module reloading but looking at browserifys require source i found out every module can actually access the module cache using arguments[5] its a object with a layout something like {numericModuleId: {exports: /* actual module exports */ }, /*.. other modules*/} so i wrote me a little function for removing something from the module cache.

var moduleCache = arguments[5];
function clearFromCache(instance)
{
    for(var key in moduleCache)
    {
        if(moduleCache[key].exports == instance)
        {
            delete moduleCache[key];
            return;
        }
    }
    throw "could not clear instance from module cache";
}

note you parse the module exports to the clear function not the path. basically you can do

//foo.js
var bar = require("./bar.js");
clearFromCache(bar);
var bar = require("./bar.js");

//bar.js
alert("hi");

and it will show 2 alerts.

i cant say often enough that doing it this way is a bad idea and may have some results like infinite loops etc.

All 5 comments

Not sure if this is the recommended way, because my use-case was a bit unusual. But I was able to get something like this working with the invalidate function:

b.invalidate(require.resolve('name-of-module'))

Thank you for the reply, but I cannot find any information about this; is it a browserify's method?

Okay so i know this is not the way its ment to be done and i really think browserify should add some sort of module reloading but looking at browserifys require source i found out every module can actually access the module cache using arguments[5] its a object with a layout something like {numericModuleId: {exports: /* actual module exports */ }, /*.. other modules*/} so i wrote me a little function for removing something from the module cache.

var moduleCache = arguments[5];
function clearFromCache(instance)
{
    for(var key in moduleCache)
    {
        if(moduleCache[key].exports == instance)
        {
            delete moduleCache[key];
            return;
        }
    }
    throw "could not clear instance from module cache";
}

note you parse the module exports to the clear function not the path. basically you can do

//foo.js
var bar = require("./bar.js");
clearFromCache(bar);
var bar = require("./bar.js");

//bar.js
alert("hi");

and it will show 2 alerts.

i cant say often enough that doing it this way is a bad idea and may have some results like infinite loops etc.

@M4GNV5 dude you're awesome. I wish there was require.cache exposed like in Node so that it can be cleared.

glad i could help ^^

Was this page helpful?
0 / 5 - 0 ratings

Related issues

saeedseyfi picture saeedseyfi  路  6Comments

QuantumKing picture QuantumKing  路  9Comments

super-cache-money picture super-cache-money  路  10Comments

ericmorand picture ericmorand  路  6Comments

3rd-Eden picture 3rd-Eden  路  10Comments