2

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

Perry
  • 264
  • 3
  • 16
Alessandro Minoccheri
  • 35,521
  • 22
  • 122
  • 171
  • Please read the documentation for nodejs modules and the use of the `require` function. Also, you might consider exploring `npmjs.org` to see which external libraries are available to you. – Rob Raisch May 14 '14 at 01:02
  • no i want to include library i know the existance of npmjs.org my question us not where to find but how create npm with external library @RobRaisch – Alessandro Minoccheri May 14 '14 at 05:06

2 Answers2

3

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.

Mohamed Allal
  • 17,920
  • 5
  • 94
  • 97
1

When one requires a module on nodejs, the content of module.exports is returned. So, one can return a function (as you do on your example) or an object, as in

in module.js:

module.exports={
    func:function(){ return true; },
    val:10,
    ...
}

So that, in the requiring file, you can:

 var m=require('module');

 assert(m.func()===true);
 assert(10===m.val);

This is explained in the nodejs documentation under Modules

So if you have an external JS library that exposes three functions: a, b, and c, you might wrap them as:

module.exports={
    exportedA:lib.a,
    exportedB:lib.b,
    exportedC:lib.c
};

lib.a=function(){ ... };

lib.b=function(){ ... };

lib.c=function(){ ... };
Rob Raisch
  • 17,040
  • 4
  • 48
  • 58
  • I know modules, but is possible to create a module with an external library? I have to get the library and copy inside a module directly? – Alessandro Minoccheri May 14 '14 at 16:21
  • What do you mean by "an external library"? JavaScript code or binary? If JavaScript, yes, you would need to expose whatever functions you wished to use by wrapping them in the `module.exports` structure. – Rob Raisch May 14 '14 at 17:12
  • for example phantomjs or casperjs an entire library to use its method in my npm. – Alessandro Minoccheri May 14 '14 at 17:27
  • Then you would need to expose whatever function or functions you need access to by including them in an external js file, wrapped in the previously mentioned structure. – Rob Raisch May 14 '14 at 17:31
  • can you please improve a simple example to understand weel what you mean? thanks – Alessandro Minoccheri May 14 '14 at 17:34
  • Sure, I'll expand on the original answer. – Rob Raisch May 14 '14 at 17:52
  • ok well this is how modules works with export and ok. But how can I for example insert phanotmjs inside this structure? It's very unclear for me.. :( sorry – Alessandro Minoccheri May 14 '14 at 18:28
  • the question is clear. using a complete library within your node apps. In others words, it's just how we #include in c++ or include in php. require() in node it's different, every file is a module, it wrap it in an object. and then how i get such a library. a simple thing is a library of one file size. and that library contain 120 function for example. how i include it in the node.js app, in one of my files directly. main.js for example. – Mohamed Allal Feb 08 '18 at 00:58