import.meta.main, neat!

if (import.meta.main) main();

import.meta.main, neat!
Photo by Allison Saeng / Unsplash

import.meta.main is now available

Boolean value available in ECMAScript modules, which can be used to detect whether the current module was the entry point of the current process.

export function foo() {  
  return 'Hello, world';
}
  
function main() {  
  const message = foo();  
  console.log(message);
}
if (import.meta.main) main();

// `foo` can be imported from another module without possible side-effects from `main`

Read more