The instanceof operator always returns false when used with the values from imported modules.
const moduleFromString = require("module-from-string");
const code = "throw new Error();";
try {
const x = moduleFromString.requireFromString(code);
} catch (err) {
console.log(Object.getPrototypeOf(err).name === Error.prototype.name);
console.log(err instanceof Error);
console.log(err instanceof Object);
}
It outputs:
Or:
const moduleFromString = require("module-from-string");
const code = "exports.obj = {}";
const x = moduleFromString.requireFromString(code);
console.log(Object.getPrototypeOf(x.obj).name === Object.prototype.name);
console.log(x.obj instanceof Object);
Outputs:
Using importFromString yields the same results. It's a side effect of using runInNewContext instead of runInThisContext (or providing context in case of the vm.Module). If this behavior is not intentional, I would be glad to fix it.
The
instanceofoperator always returnsfalsewhen used with the values from imported modules.It outputs:
truefalsefalseOr:
Outputs:
truefalseUsing
importFromStringyields the same results. It's a side effect of usingrunInNewContextinstead ofrunInThisContext(or providing context in case of thevm.Module). If this behavior is not intentional, I would be glad to fix it.