Note that there are some explanatory texts on larger screens.

plurals
  1. POSynchronously load a dependency in node.js
    text
    copied!<p>I'm loading a node library in script and immediately after loading some customization that depends on that library:</p> <pre><code>var somelib = require('somelib'); // some customizations made to // somelib's methods require('./somelib.custom'); somelib.SomeObject = ... ^ ReferenceError: somelib is not defined </code></pre> <p>I keep getting an exception since the loading is done asynchronously and the second <code>require</code> happens before the first is namespaced correctly. What's a good way to resolve this? thanks.</p> <h3>EDIT: My original code</h3> <p>I'm trying to create a PNG image from json data using <a href="https://github.com/kangax/fabric.js" rel="nofollow"><code>fabric.js</code></a> node package (building on the <a href="http://fabricjs.com/fabric-intro-part-4/#node" rel="nofollow">article</a> in package site). This is done by loading the server-side fabric canvas with JSON data that was originally generated on the client, then writing to a file stream:</p> <pre><code>var path = require('path'); var fs = require('fs'); var fabric = require('fabric').fabric; require('./fabric.custom'); var canvas = fabric.createCanvasForNode(400, 400); var outfile = fs.createWriteStream("image.png"); var filepath = "/path/to/file.json"; fs.readFile(filepath, 'utf8', function(err, json) { canvas.loadFromJSON(json, function() { var stream = canvas.createPNGStream(); stream.on('data', function(chunk) { outfile.write(chunk); }); }); }); </code></pre> <p>The "fabric.custom" file holds several custom fabric canvas objects that override some fabric prototype defaults. They work well on the client, and are needed to properly render the canvas. It looks something like this:</p> <pre><code>fabric.TextBox = fabric.util.createClass(fabric.Text, { type: 'text-box', // more object specific stuff ... }); </code></pre> <p>Thanks.</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