import.meta
import.meta
元属性向 JavaScript 模块公开上下文特定的元数据。它包含有关模块的信息,例如模块的 URL。
¥The import.meta
meta-property exposes context-specific metadata to a JavaScript module. It contains information about the module, such as the module's URL.
语法
值
¥Value
import.meta
对象由主机环境创建,作为可扩展的 null
-原型 对象,其中所有属性都是可写、可配置和可枚举的。规范没有指定要在其上定义的任何属性,但主机通常实现以下属性:
¥The import.meta
object is created by the host environment, as an extensible null
-prototype object where all properties are writable, configurable, and enumerable. The spec doesn't specify any properties to be defined on it, but hosts usually implement the following properties:
描述
¥Description
import.meta
语法由关键字 import
、一个点和标识符 meta
组成。因为 import
是 保留字,而不是标识符,所以这不是 属性访问器,而是一种特殊的表达式语法。
¥The import.meta
syntax consists of the keyword import
, a dot, and the identifier meta
. Because import
is a reserved word, not an identifier, this is not a property accessor, but a special expression syntax.
import.meta
元属性在 JavaScript 模块中可用;在模块外部使用 import.meta
(包括模块内的 直接 eval()
)是语法错误。
¥The import.meta
meta-property is available in JavaScript modules; using import.meta
outside of a module (including direct eval()
within a module) is a syntax error.
示例
传递查询参数
¥Passing query parameters
在 import
说明符中使用查询参数允许特定于模块的参数传递,这可能是对从应用范围的 window.location
(或在 Node.js 上,通过 process.argv
)读取参数的补充。例如,使用以下 HTML:
¥Using query parameters in the import
specifier allows module-specific argument passing, which may be complementary to reading parameters from the application-wide window.location
(or on Node.js, through process.argv
). For example, with the following HTML:
<script type="module">
import "./index.mjs?someURLInfo=5";
</script>
index.mjs
模块能够通过 import.meta
检索 someURLInfo
参数:
¥The index.mjs
module is able to retrieve the someURLInfo
parameter through import.meta
:
// index.mjs
new URL(import.meta.url).searchParams.get("someURLInfo"); // 5
当一个模块导入另一个模块时,这同样适用:
¥The same applies when a module imports another:
// index.mjs
import "./index2.mjs?someURLInfo=5";
// index2.mjs
new URL(import.meta.url).searchParams.get("someURLInfo"); // 5
Node.js 中的 ES 模块实现支持解析包含查询参数(或哈希)的模块说明符,如后一个示例所示。但是,当通过 CLI 命令(如 node index.mjs?someURLInfo=5
)指定模块时,你不能使用查询或哈希,因为 CLI 入口点使用更类似于 CommonJS 的解析模式,将路径视为文件路径而不是 URL。要将参数传递给入口点模块,请使用 CLI 参数并通过 process.argv
读取它们(如 node index.mjs --someURLInfo=5
)。
¥The ES module implementation in Node.js supports resolving module specifiers containing query parameters (or the hash), as in the latter example. However, you cannot use queries or hashes when the module is specified through the CLI command (like node index.mjs?someURLInfo=5
), because the CLI entrypoint uses a more CommonJS-like resolution mode, treating the path as a file path rather than a URL. To pass parameters to the entrypoint module, use CLI arguments and read them through process.argv
instead (like node index.mjs --someURLInfo=5
).
解析相对于当前文件的文件
¥Resolving a file relative to the current one
在 Node.js CommonJS 模块中,有一个 __dirname
变量,其中包含当前模块所在文件夹的绝对路径,这对于解析相对路径很有用。但是,除了 import.meta
之外,ES 模块不能有上下文变量。因此,要解析相对文件,你可以使用 import.meta.url
。请注意,这使用 URL 而不是文件系统路径。
¥In Node.js CommonJS modules, there's a __dirname
variable that contains the absolute path to the folder containing current module, which is useful for resolving relative paths. However, ES modules cannot have contextual variables except for import.meta
. Therefore, to resolve a relative file you can use import.meta.url
. Note that this uses URLs rather than filesystem paths.
之前(CommonJS):
¥Before (CommonJS):
const fs = require("fs/promises");
const path = require("path");
const filePath = path.join(__dirname, "someFile.txt");
fs.readFile(filePath, "utf8").then(console.log);
之后(ES 模块):
¥After (ES modules):
import fs from "node:fs/promises";
const fileURL = new URL("./someFile.txt", import.meta.url);
fs.readFile(fileURL, "utf8").then(console.log);
规范
Specification |
---|
ECMAScript Language Specification # prod-ImportMeta |
HTML Standard # hostgetimportmetaproperties |
浏览器兼容性
BCD tables only load in the browser