Can I specify additional module dependencies in npm package.json?

I have a service where I want to allow users during installation to specify which persistence mechanism to use, i.e. based on files, MongoDB or Redis, and I'm looking for some npm magic where you load only the necessary modules (no, mongodb or redis, respectively).

Is it possible? I cannot find any parameters other than defining dependencies and devDependencies in package.json, and this is not suitable for this.

Also note that while the mongodb and redis modules may be relatively small, consider an alternative case where you might need Java to communicate RMI.

Thanks!

+5
source share
1 answer

You might want to use a post-install script, and then install them.

You can programmatically install objects using the npm module .

So you can do something like this:

 var npm = require('npm'); // make sure npm is in your package.json! npm.load({/* some object properties, if needed */}, function(err) { if (err) {return handleError(err)} if (usingMongoDB) { npm.commands.install(['mongodb'], function(err){ if (err) {return handleError(err)} console.log('mongodb successfully installed'); }); }); 

Now I have never done anything like this, so I recommend looking at the documentation for software npm install , as well as load .

+2
source

All Articles