Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test if object is es6 module

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?

like image 283
philipp Avatar asked Aug 08 '26 10:08

philipp


1 Answers

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

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

See docs for Symbol.toStringTag

like image 200
dvv Avatar answered Aug 09 '26 23:08

dvv