Node.js有条件的要求 [英] Node.js conditional require
本文介绍了Node.js有条件的要求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在最新版本的Node中支持插件 c
。
有条件加载它的最佳方法是什么?
Consider plugin c
is supported in recent versions of Node.
What would be the best way to conditionally load it?
module.exports = {
plugins: [
require("a"),
require("b"),
[require("c"), { default: false }] //only if node version > 0.11
]
};
推荐答案
确保添加 semver
包作为依赖项,然后:
Make sure you add the semver
package as a dependenc, then:
var semver = require("semver")
var plugins = [
require("a"),
require("b"),
];
if(semver.gt(process.version, "0.11")){
plugins.push(require("c"));
}
module.exports = {
plugins: plugins
};
此代码使用 process.version $ c检查节点版本$ c>,如果支持,则将所需的插件附加到列表中。
This code checks for the node version using process.version
, and appends the required plugin to the list if it is supported.
这篇关于Node.js有条件的要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文