import.meta.resolve(specifier[, parent])


稳定性: 1 - 实验

此特性仅在启用 --experimental-import-meta-resolve 命令标志时可用。

  • specifier <string> 相对于 parent 解析的模块说明符。
  • parent <string> | <URL> 要解析的绝对的父模块 URL。 如果未指定,则使用 import.meta.url 的值作为默认值。
  • 返回: <Promise>

提供作用域为每个模块的模块相关解析函数,返回 URL 字符串。

const dependencyAsset = await import.meta.resolve('component-lib/asset.css');

import.meta.resolve 还接受第二个参数,它是从中解析的父模块:

await import.meta.resolve('./dep', import.meta.url);

此函数是异步的,因为 Node.js 中的 ES 模块解析器是允许异步的。

Stability: 1 - Experimental

This feature is only available with the --experimental-import-meta-resolve command flag enabled.

  • specifier <string> The module specifier to resolve relative to parent.
  • parent <string> | <URL> The absolute parent module URL to resolve from. If none is specified, the value of import.meta.url is used as the default.
  • Returns: <Promise>

Provides a module-relative resolution function scoped to each module, returning the URL string.

const dependencyAsset = await import.meta.resolve('component-lib/asset.css');

import.meta.resolve also accepts a second argument which is the parent module from which to resolve from:

await import.meta.resolve('./dep', import.meta.url);

This function is asynchronous because the ES module resolver in Node.js is allowed to be asynchronous.