跳转至

import.meta

The import.meta meta property is an Object that contains the following properties.

import.meta.url

  • The absolute file: URL of the module.

This is defined exactly the same as it is in browsers providing the URL of the current module file.

This enables useful patterns such as relative file loading:

import { readFileSync } from "node:fs";
const buffer = readFileSync(new URL("./data.proto", import.meta.url));

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

Stability: 1 - Experimental

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

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

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.