Skip to content

Objects from imported modules have different core types, making instanceof useless #16

Description

@phorks

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:

  • true
  • false
  • false

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:

  • true
  • false

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions