如何将外部库导入nodejs [英] how to import external library to nodejs

查看:125
本文介绍了如何将外部库导入nodejs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何将外部库导入 nodejs.例如我想要 phanotmjs 库(我知道 arelady 存在一个 npm 来获取 phantomjs,但这只是一个例子).

I would like to know how can I import an external library to nodejs. For example I would like to have phanotmjs library (I know that arelady exist an npm to get phantomjs, but is only an example).

我认为一种方法是获取库的源文件并将其包含到这样的模块中:

I think that a way was to get the source file of library and include it into a module like that:

module.exports = function (name, cb) {
   //source code of library
});

但我认为是错误的方式.

But I think is a wrong way.

如何将外部库包含到 nodejs 项目中以在其中使用它的功能?

How can I include external library to nodejs project to use it inside it with its functionality?

谢谢

推荐答案

如果不导出,一种不优雅的方法是复制整个库,在节点文件的底部.讨厌你可能已经想到了.这也有不好的地方.您将无法在所有不同的文件中重复使用它.

Without exporting, a no elegant way is to copy past the entire library, in the bottom of your node file. nasty you may already thought about it. there is also a bad thing about that. you will not be able to reuse it in all different files.

另一种方法是在每次需要函数时沿工作流程导出文件.我认为这没问题.

The other way is to export the files along your workflow every time you need a function. And i think this is ok.

否则,您可以这样编写导出:

module.exports = {
    removeElementFromArray_Mutate,
    hasClass,
    hasClass_ONEtest,
    removeClassFromAll,
    addClass,
    removeClass
};

你可以用 node.js 做到这一点.所有这些都是以这种方式声明的普通函数:

you can do that with node. all of those are normal function declared this way:

function removeClassFromAll(DOMobject, classes){
    for(let i = 0; i < DOMobject.length; i++){
        removeClass(DOMobject[i], classes);
    }
}

function hasClass_ONEtest(DOMElement, classe) {
    let allClasses = DOMElement.className.split(/\s+/);
    for(let i = 0; i < allClasses.length; i++){
       if(allClasses[i].trim() === classe){
           return true;
       }
    }
    return false;
}

function hasClass(DOMElement, classes) {
    if (typeof classes === 'string') {
        return hasClass_ONEtest(DOMElement, classes);
    } else { // multiple classes as array
        for (let i = 0; i < classes.length; i++) {
            if (!hasClass_ONEtest(DOMElement, classes[i])) {
                return false;
            }
        }
        return true;
    }
}

通过这种方式,您可以编写一个快速脚本来解析所有文件,并取出函数的定义,如果您无法手动完成.您可以使用正则表达式来加快速度.你需要两种模式.第一个用于 function name(,第二个用于 name = function(.希望有帮助!

this way you can write a quick script that parse all the file, and take out the definitions of the functions, if you can't do it manually. You can use regex, to speed up that. you need two patterns. the first for function name( and the second for name = function(. Hope that was helpful!

问题更多是关于 nodejs 是否包含一种方法.目前没有.它可能在未来.您可能还会看到这个 我如何包含一个另一个 JavaScript 文件中的 JavaScript 文件?.不过这可能无济于事.

the question was more about if there is a way included with nodejs. There is none at the moment. it may be in the future. You may also see this How do I include a JavaScript file in another JavaScript file?. It may not help though.

这篇关于如何将外部库导入nodejs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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