检测是否通过Node中的命令行运行ES Module [英] detect whether ES Module is run from command line in Node
问题描述
在Node中使用CommonJS模块时,可以使用require.main === module
从命令行检测脚本是否正在运行.
When using CommonJS modules in Node, you can detect whether a script is being run from the command line using require.main === module
.
在Node中使用ES模块(带有--experimental-modules
标志)时,从命令行检测脚本是否正在运行的等效方法是什么?
What is an equivalent way to detect whether a script is being run from the command line when using ES Modules in Node (with the --experimental-modules
flag)?
推荐答案
目前还没有-(仍在试验中!).尽管普遍的看法是,无论如何,这种检查都是不好的做法,您应该只为库和可执行文件提供单独的脚本,但有一个想法为此目的提供了一个布尔型import.meta.main
属性.
There is none - yet (it's still experimental!). Although the prevailing opinion is that such a check is a bad practice anyway and you should just provide separate scripts for the library and the executable, there is an idea to provide a boolean import.meta.main
property for this purpose.
这篇关于检测是否通过Node中的命令行运行ES Module的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!