I'm encountering a strange behaviour with instanceof in NodeJs. I'm trying to creating a module which throws exceptions which can be caught by consuming modules and dealt with based on the exception type. However the call to test the type "error instanceof mod.MyException" is always returning false.
mymodule.js:
function MyException() {}
MyException.prototype = new Error();
MyException.prototype.constructor = MyException;
function Foo() {
throw new MyException();
}
exports.Foo = Foo;
exports.MyException = MyException;
myconsumingmodule.js
var mod = require('mymodule');
try {
mod.Foo();
} catch (error) {
if (error instanceof mod.MyException) { // always returns false
//do error specific handling (never executes)
return;
}
// generic handling
}
If I do the instanceof test in mymodule.js like so it returns true
mymodule.js
var a = new MyException()
console.log(a instanceof MyException); //true
However if I create an exception in the consuming module and test it it correctly identifies it as MyException, it just doesn't when comparing it against an exception that was created by the other module.
myconsumingmodule.js
var mod = require('mymodule')
var b = new mod.MyException();
console.log(b instanceof mod.MyException); // true
FOUND IT
To use your example, in file one.js
I had:
var mod = require('mymodule');
but in another file two.js
I had:
var mod = require('Mymodule');
and everything seemed to work exactly the same, except when you compared an error created from one.js
using instanceof
in two.js
!.
Absurdly, node.js allows you to do a case-insensitive require, except it's not really case-insensitive, it actually requires your file twice, which is why instanceof
returns false
– the constructors are two different functions from two different module instances!
TL;DR
Make sure all your require
strings are case-consistent.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With