Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's what I think is the 'rightest' answer for this situation.</p> <p>Say you have a script file called <code>quadtree.js</code>.</p> <p>You should build a custom <code>node_module</code> that has this sort of directory structure...</p> <pre><code>./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 </code></pre> <p>Everything in your <code>./node_modules/quadtree/quadtree-lib/</code> directory are files from your 3rd party library. </p> <p>Then your <code>./node_modules/quadtree/index.js</code> file will just load that library from the filesystem and do the work of exporting things properly.</p> <pre><code>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 </code></pre> <p>Now you can use your <code>quadtree</code> module like any other node module...</p> <pre><code>var qt = require('quadtree'); qt.QuadTree(); </code></pre> <hr> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload