Load “Vanilla” Javascript Libraries into Node.js

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.

Leave a Comment