import.meta.main


稳定性: 1.0 - 早期开发

  • 类型:<boolean> 当当前模块是当前进程的入口时,true;否则就是 false

相当于 CommonJS 中的 require.main === module

🌐 Equivalent to require.main === module in CommonJS.

类似于Python的“name== ”main“”。

🌐 Analogous to Python's __name__ == "__main__".

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`