在 deno 中,我可以知道我的脚本是直接 运行 还是被另一个脚本加载吗?
Can I know, in deno, if my script is being run directly or being loaded by another script?
在节点中并行:Can I know, in node.js, if my script is being run directly or being loaded by another script?
我正在寻找一种方法来判断 deno 脚本是直接 运行 还是被另一个模块导入。这在 deno 中可能吗?如果有怎么办?
您必须使用 import.meta.main
才能知道脚本是否是入口点。
main.js
import child from './child.js';
console.log('Main', import.meta.main);
child.js
export default 'foo';
console.log('child', import.meta.main);
现在当你执行:
deno run main.js
您将获得:
child: false
Main: true
在节点中并行:Can I know, in node.js, if my script is being run directly or being loaded by another script?
我正在寻找一种方法来判断 deno 脚本是直接 运行 还是被另一个模块导入。这在 deno 中可能吗?如果有怎么办?
您必须使用 import.meta.main
才能知道脚本是否是入口点。
main.js
import child from './child.js';
console.log('Main', import.meta.main);
child.js
export default 'foo';
console.log('child', import.meta.main);
现在当你执行:
deno run main.js
您将获得:
child: false
Main: true