我可以在 npm package.json 中指定可选的模块依赖项吗? [英] Can I specify optional module dependencies in npm package.json?

查看:81
本文介绍了我可以在 npm package.json 中指定可选的模块依赖项吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一项服务,我想允许用户在安装时指定要使用的持久性引擎,即基于文件的、MongoDB 或 Redis,我正在寻找一些 npm 魔法,您只需下载必要的模块(分别为无、mongodb 或 redis).

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).

这可能吗?除了在 package.json 中定义依赖项和 devDependencies 之外,我找不到任何选项,这不适合于此.

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.

另请注意,虽然 mongodb 和 redis 模块可能相对较小,但请考虑另一种情况,您可能需要使用 Java 进行 RMI 通信.

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.

谢谢!

推荐答案

您可能想要使用安装后脚本,然后安装它们.

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

您可以使用 npm 模块 以编程方式安装内容.

You can install things using the npm module programmatically.

所以,你可以这样做:

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');
    });
});

现在,我从来没有做过这样的事情,所以我建议你查看程序化文档npm install,还有 load.

Now, i have never done something like this, so i recommend you look at the documentation for programmatic npm install, and also load.

这篇关于我可以在 npm package.json 中指定可选的模块依赖项吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆