Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
970 views
in Technique[技术] by (71.8m points)

node.js - how to import external library to nodejs

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 it is a wrong way of doing it.

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

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

Otherwise to answer that, you can write the export this way:

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

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

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!

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...