Using `instanceof` on objects created with constructors from deep npm dependencies : <h2>Background:</h2>

<p>I have an npm module that I have common error handling code in, including a custom error:</p>

<pre><code>function CustomError () { /* ... */ }
CustomError.prototype = Object.create(Error.prototype);
CustomError.prototype.constructor = CustomError;
module.exports = CustomError;
</code></pre>

<p>I have some other modules (let's call them <strong>'module-a'</strong> and <strong>'module-b'</strong>) which both depend on the error handling module.</p>

<p>I also have some code with uses Bluebirds "filtered catch" functionality:</p>

<pre><code>doSomething
.catch(CustomError, () =&gt; { /* ... */ });
</code></pre>

<h2>The issue:</h2>

<p>After some debugging I've discovered (somewhat obviously in hindsight) that errors created in <strong>'module-a'</strong> are not instances of errors created by <strong>'module-b'</strong>. This is because both modules have their own copy of the JS file containing the <code>CustomError</code> constructor, which are both run independently.</p>

<p>I would rather not have to resort to my current solution, which is basically:</p>

<pre><code>CustomError.isCustomError = e =&gt; e.constructor.toString() === CustomError.toString();
</code></pre>

<p>and then:</p>

<pre><code>doSomething
.then(CustomError.isCustomError, () =&gt; { /* ... */ });
</code></pre>

<p>This is clearly flimsy, and will fall apart if the versions fall out of sync.</p>

<h2>So...</h2>

<p>Is there some way to ensure that <strong>'module-a'</strong> and <strong>'module-b'</strong> both use the same instance of the constructor? Or another, less fragile solution.</p>
