Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

node.js

npm

I have a service where I want to allow users at install-time to specify which persistence engine to use, i.e. file-based, MongoDB, or Redis, and I'm looking for some npm magic where you only download the necessary modules (none, mongodb, or redis, respectively).

Is this possible? I can't find any options other than defining dependencies and devDependencies in package.json, and that's not appropriate for this.

Note also that while the mongodb and redis modules may be relatively small, consider an alternate case where you may optionally need Java for RMI communication.

Thanks!

like image 459
anthonyserious Avatar asked Oct 19 '22 19:10

anthonyserious


1 Answers

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

You can install things using the npm module programmatically.

So, you might 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 something like this, so i recommend you look at the documentation for programmatic npm install, and also load.

like image 84
Scimonster Avatar answered Nov 15 '22 07:11

Scimonster