【问题标题】:Can I know, in deno, if my script is being run directly or being loaded by another script?在 deno 中,我可以知道我的脚本是直接运行还是被另一个脚本加载?
【发布时间】:2020-06-07 18:38:27
【问题描述】:

并行节点:Can I know, in node.js, if my script is being run directly or being loaded by another script?

我正在寻找一种方法来判断 deno 脚本是直接运行还是由另一个模块导入。这在 deno 中可能吗?如果有怎么办?

【问题讨论】:

    标签: deno


    【解决方案1】:

    您必须使用import.meta.main 来了解脚本是否是入口点。

    ma​​in.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
    

    【讨论】:

      猜你喜欢
      • 2012-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 2019-09-03
      相关资源
      最近更新 更多