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


稳定性: 1 - 实验性

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

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

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

提供一个相对于模块的解析函数,作用域限定在每个模块内,返回 URL 字符串。

【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 也接受第二个参数,它是用来指定解析来源的父模块:

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

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

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