finding modules matching a name glob

56 views
Skip to first unread message

Mark Hahn

unread,
Apr 21, 2012, 12:04:16 AM4/21/12
to nodejs
I need a plug-in system for my app and I'd like for my user to be able to install a plug-in by just doing the standard `npm install foo` where foo is the name of the module.  When my app starts I want to find all plugins for my app and "require" them without knowing their names.

I'm thinking that any and all modules with a name matching *_myapp_plugin (I don't know my app's name yet)  would be loaded (required).  The user of my app would then be able to enable/disable these plugins through the UI.

I considered walking the dir tree looking for node_module folders, but then I started thinking about all the complications.  Maybe I should steal code from node require.  Of course if node allowed wild cards in the require function that would be ideal.

I'd be happy to make this plug-in system a module.  Node doesn't have enough modules yet.


Dominic Tarr

unread,
Apr 21, 2012, 8:06:55 AM4/21/12
to nod...@googlegroups.com
there are lots of places that the installed modules could turn up. the best might be to use npm ls (or link to npm), that will find you all the dirs, and the have plugin authors add a field to their package.json that indicates it is a plugin.



--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Mark Hahn

unread,
Apr 21, 2012, 1:51:30 PM4/21/12
to nod...@googlegroups.com
So doing an `npm ls` from a process is the only way to go?  Whatever it takes.

Mark Hahn

unread,
Apr 21, 2012, 1:52:09 PM4/21/12
to nod...@googlegroups.com
A discovery process for modules would really be nice.  Consider this a feature request.

Mike Pilsbury

unread,
Apr 21, 2012, 2:04:42 PM4/21/12
to nod...@googlegroups.com
So doing an `npm ls` from a process is the only way to go?  Whatever it takes.


phidelta

unread,
Apr 22, 2012, 8:26:52 AM4/22/12
to nodejs
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

Mark Hahn

unread,
Apr 22, 2012, 2:37:47 PM4/22/12
to nod...@googlegroups.com
 What I ended up doing is simply defining a folder which all plugins have to be in. 

That seems to be a good minimalist approach.  

One requirement for this project is for plugin developers to be able to easily publish their plugins so that every app user can select which plugins to use.  So some kind of public repository, or registry, is needed to drop in plugins.  So I either use the npm registry or somewhat duplicate it's functionality.

Then I need the modules installed locally.  Putting them all in a single folder works for this.

Reply all
Reply to author
Forward
0 new messages