import.meta.main
稳定性: 1.0 - 早期开发
¥Stability: 1.0 - Early development
-
类型:<boolean> 当前模块为当前进程的入口点时,
true会被调用;false否则。¥Type: <boolean>
truewhen the current module is the entry point of the current process;falseotherwise.
相当于 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`