Created
September 3, 2014 13:33
-
-
Save sbellone/3947632a42be73553abb to your computer and use it in GitHub Desktop.
Install 'winston' 0.6.2, require it, then clean all module cache, install 'winston' 0.7.2 and require it. It will fail because 'winston' 0.6.x uses 'async' 0.1 whereas 'winston' 0.7.x uses 'async' 0.2, which has changed its main file. Since the main file name is kept in cache in the variable packageMainCache (in module.js), the module will not b…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var npm = require('npm'); | |
npm.load({}, function (er) { | |
if (er) console.log(er) | |
npm.commands.install(["[email protected]"], function (er, data) { | |
if (er) { | |
console.log("npm install failed: " + er); | |
} | |
else { | |
var winston = require('winston'); | |
console.log("Winston version: " + winston.version); | |
upgradeWinstonVersion(); | |
} | |
}); | |
npm.on("log", function (message) { console.log(message) }); | |
}); | |
function cleanModuleCache(moduleName) { | |
var mod = require.resolve(moduleName); | |
if (mod && ((mod = require.cache[mod]) !== undefined)) { | |
(function run(mod) { | |
mod.children.forEach(function (child) { | |
run(child); | |
}); | |
delete require.cache[mod.id]; | |
})(mod); | |
} | |
Object.keys(module.constructor._pathCache).forEach(function(cacheKey) { | |
if (cacheKey.indexOf(moduleName) > 0) { | |
delete module.constructor._pathCache[cacheKey]; | |
} | |
}); | |
}; | |
function upgradeWinstonVersion() { | |
cleanModuleCache('winston'); | |
npm.commands.install(["[email protected]"], function (er, data) { | |
if (er) { | |
console.log("npm install failed: " + er); | |
} | |
else { | |
var winston = require('winston'); | |
console.log("Winston version: " + winston.version); | |
} | |
}); | |
} |
It works indeed! But I think will look at other possibilities for what I wanted to achieve, and I will see if the issue get reopened. Thanks for your investigation!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's an extremely ugly workaround, but it works, till #8266 get reopened, discussed and hopefully resolved.
Override the
Module._findPath
function, along with all the helper functions that it depends on, I tested this and it works. Good luck.https://gist.github.com/akhoury/85a4a84ed244c9c2053a