import.meta.main


稳定性: 1.0 - 早期开发

¥Stability: 1.0 - Early development

  • <boolean> 当前模块为当前进程的入口点时,true 会被调用;false 否则。

    ¥<boolean> true when the current module is the entry point of the current process; false otherwise.

相当于 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`