Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load "Vanilla" Javascript Libraries into Node.js

There are some third party Javascript libraries that have some functionality I would like to use in a Node.js server. (Specifically I want to use a QuadTree javascript library that I found.) But these libraries are just straightforward .js files and not "Node.js libraries".

As such, these libraries don't follow the exports.var_name syntax that Node.js expects for its modules. As far as I understand that means when you do module = require('module_name'); or module = require('./path/to/file.js'); you'll end up with a module with no publicly accessible functions, etc.

My question then is "How do I load an arbitrary javascript file into Node.js such that I can utilize its functionality without having to rewrite it so that it does do exports?"

I'm very new to Node.js so please let me know if there is some glaring hole in my understanding of how it works.


EDIT: Researching into things more and I now see that the module loading pattern that Node.js uses is actually part of a recently developed standard for loading Javascript libraries called CommonJS. It says this right on the module doc page for Node.js, but I missed that until now.

It may end up being that the answer to my question is "wait until your library's authors get around to writing a CommonJS interface or do it your damn self."

like image 319
Chris W. Avatar asked Mar 02 '11 17:03

Chris W.


People also ask

Can I use vanilla JS in node?

You could maby be able to use vanilla javascript for the server side stuff. But you should atleast look into Nodejs or Deno since those are made for being used on the server side and just vanilla javascript is made for use in the browser.

Can I use JavaScript in node JS?

Node. js allows you to run JavaScript on the server.


1 Answers

Here's what I think is the 'rightest' answer for this situation.

Say you have a script file called quadtree.js.

You should build a custom node_module that has this sort of directory structure...

./node_modules/quadtree/quadtree-lib/ ./node_modules/quadtree/quadtree-lib/quadtree.js ./node_modules/quadtree/quadtree-lib/README ./node_modules/quadtree/quadtree-lib/some-other-crap.js ./node_modules/quadtree/index.js 

Everything in your ./node_modules/quadtree/quadtree-lib/ directory are files from your 3rd party library.

Then your ./node_modules/quadtree/index.js file will just load that library from the filesystem and do the work of exporting things properly.

var fs = require('fs');  // Read and eval library filedata = fs.readFileSync('./node_modules/quadtree/quadtree-lib/quadtree.js','utf8'); eval(filedata);  /* The quadtree.js file defines a class 'QuadTree' which is all we want to export */  exports.QuadTree = QuadTree 

Now you can use your quadtree module like any other node module...

var qt = require('quadtree'); qt.QuadTree(); 

I like this method because there's no need to go changing any of the source code of your 3rd party library--so it's easier to maintain. All you need to do on upgrade is look at their source code and ensure that you are still exporting the proper objects.

like image 102
Chris W. Avatar answered Sep 27 '22 22:09

Chris W.