Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

202
Vistas
How to know whether a piece of JS is executed in an ES Module or a regular script?

I want to know whether a piece of JavaScript is executed in an ES module or a simple script.

This is what I tried so far:

function isEsm1() {
  try {
    // Script gives a syntax error during parsing when script is not an esm
    return Boolean(import.meta.url); 
  } catch(err) {
    return false;
  }
}

function isEsm2() {
  // will always return false, because `eval` always seems to be executed in regular script context
  try {
    return eval('Boolean(import.meta.url)'); 
  } catch(err) {
    return false;
  }
}


function isEsm3() {
  // Of course doesn't work, but had to try 😉
  return 'meta' in import; 
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Is the regular script executed in the browser or in another context?

In a browser, how about:

var anyvar = {};
var inModule = anyvar === window.anyvar;

If you're in a module, you are not declaring anything on the window...

In NodeJS you could do something similar with global or this:

let inModule = this === module.exports..

Did not try it yet.. but should work I guess...

After testing, just the check for this === undefined is enough to test if you're executing in or out of a module..

Inside a module, this is undefined (as per spec). In global scope this points to global this, which is the window object in the case of a browser context...

about 4 years ago · Juan Pablo Isaza Denunciar

0

Thanks to the discussion in John Gorter's answer, I think we've found a way.

console.log('In module: ' + (this === undefined));

It's as simple as that. Inside a module (and only inside a module (I hope)), this will be undefined. I found it in the v8 documentation here: https://v8.dev/features/modules#intro

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda