Is it possible to determine the directory where required
scripts come from?
My ultimate goal is to...
// return the directory where `mysql` module is found
var mysqldir = requiredir('mysql')
if(fs.existsSync(mysqldir+"/README.md")){
console.log(fs.readFileSync(mysqldir+"/README.md").toString())
}
So I can have a handy reference for all these modules, and hopefully people in the enlightened future, will include module docs/cheatsheets intended to be accessed in this manner
required.resolve(...)
may be what you're looking for:
function requiredir(id) {
return path.dirname(require.resolve(id));
}
Though, this will follow the main
setting:
> requiredir('grunt')
.../node_modules/grunt/lib/
(Update: Documentation for the API isn't available anymore, so its use likely isn't supported.) Another option may be to use npm
's view
, though this is limited to registered packages:
npm.load({}, function (err) {
npm.commands.view(['mysql', 'readme'], true, function (err, readme) {
console.log(readme);
});
});