程序入口点


🌐 Program entry point

程序的入口点是类似于说明符的字符串。如果该字符串不是绝对路径,它将作为相对于当前工作目录的路径进行解析。然后,该入口点字符串将被解析,就好像它是从当前工作目录通过 require() 请求的一样。如果没有找到对应的文件,则会抛出错误。

🌐 The program entry point is a specifier-like string. If the string is not an absolute path, it's resolved as a relative path from the current working directory. That entry point string is then resolved as if it's been requested by require() from the current working directory. If no corresponding file is found, an error is thrown.

默认情况下,解析后的路径也会被加载,就好像是通过 require() 请求的,除非满足以下条件之一——此时它会像通过 import() 请求的一样被加载:

🌐 By default, the resolved path is also loaded as if it's been requested by require(), unless one of the conditions below apply—then it's loaded as if it's been requested by import():

  • 该程序是通过命令行标志启动的,该标志强制入口点使用 ECMAScript 模块加载器加载,例如 --import
  • 该文件具有 .mjs.mts.wasm 扩展名。
  • 该文件没有 .cjs 扩展名,并且最近的父级 package.json 文件包含一个顶层 "type" 字段,其值为 "module"。

有关详细信息,请参见模块解析与加载

🌐 See module resolution and loading for more details.