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); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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!