Test if object is es6 module
Asked Answered
H

1

7

Using dynamics imports like that:

const i = import('./path/to/module.js');

yields a Promise for that module. Assuming that ./path/to/module.js looks like that:

export function foo () {}
export function bar () {}
export default function () {}

the resulting object will have such a shape:

{
  foo: …
  bar: …
  default: …
}

Right now I need to figure out if a given Object is such a module, or any other one. So: Is there any test to determine if a given object is such a es6 module, obtained by a dynamic import?

Hide answered 31/8, 2021 at 10:36 Comment(2)
If you are exporting a class from the module, you can check it using instanceOf.Brundisium
@ApoorvaChikara Thanks for that hint. But I am not after checking the exported item, I'd like to ckeck if an object is an es6 module.Hide
A
4

You should check Symbol.toStringTag property of your alleged module object:

const isModule = i[Symbol.toStringTag] == 'Module'

See docs for Symbol.toStringTag

Alsacelorraine answered 10/6, 2023 at 20:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.