The problem with doing this via npm is that npm works with the
dependencies from the package.json which is a problem for plugins,
since they are seldom known at the time the package.json is writeen.
I have done something like this for my app. What I ended up doing is
simply defining a folder which all plugins have to be in.
function requirePlugins(plugindir) {
require('fs').readdirSync(plugindir).map(function(item) {
return require('path').resolve(plugindir, item);
}).forEach(function(plugin) {
if (/\.plugin$/.test(plugin)) require(plugin);
});
}
on start-up I simply call
requirePlugins(require('path').resolve('./plugins'));
This avoids traversing a billion directories, and does what you need.
It is synchronous of course, but that is true for require anyhow and
does not matter much since it is ever only called on start-up.
Regards, Philipp