gpt4 book ai didi

node.js - 从模块调用的nodejs

转载 作者:太空宇宙 更新时间:2023-11-03 23:10:32 24 4
gpt4 key购买 nike

如何判断 require 正在调用用 Node 编写的脚本?

最佳答案

来自node docs :

Accessing the main module

When a file is run directly from Node, require.main is set to its module. That means that you can determine whether a file has been run directly by testing

 require.main === module

For a file foo.js, this will be true if run via node foo.js, but false if run by require('./foo').

Because module provides a filename property (normally equivalent to __filename), the entry point of the current application can be obtained by checking require.main.filename.

关于node.js - 从模块调用的nodejs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13829955/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com