Skip to content

Instantly share code, notes, and snippets.

@sbellone
Created September 3, 2014 13:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sbellone/3947632a42be73553abb to your computer and use it in GitHub Desktop.
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…
var npm = require('npm');
npm.load({}, function (er) {
if (er) console.log(er)
npm.commands.install(["winston@0.6.2"], 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(["winston@0.7.2"], function (er, data) {
if (er) {
console.log("npm install failed: " + er);
}
else {
var winston = require('winston');
console.log("Winston version: " + winston.version);
}
});
}
@akhoury
Copy link

akhoury commented Sep 3, 2014

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

@sbellone
Copy link
Author

sbellone commented Sep 3, 2014

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