Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท Operators

The import.meta object

Reference for import.meta in JavaScript modules: the url property, resolving relative paths, host-specific extras in browsers and Node.js, and its limits.

import.meta is an object that exists only inside JavaScript modules (files loaded with <script type="module"> or import). It carries information about the current module. The language itself does not define any properties on it; hosts add them. The one property you can count on in browsers is import.meta.url, the absolute URL of the module file.

JavaScript
const dataUrl = new URL("./data.json", import.meta.url);

Try it

JS Reading import.meta

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Example in a module script

Syntax

JavaScript
import.meta
import.meta.url

Return value: an object with a null prototype, created once per module and the same object on every access in that module. Its properties are writable and enumerable, and the object is extensible, so you can add your own, though that is rarely a good idea.

Exceptions: using import.meta outside a module (a classic script, eval, or a CommonJS file) is a SyntaxError.

Usage notes

  • import.meta.url is the module's full URL as a string (https://... in browsers, file:///... in Node.js ES modules). Combine it with new URL(relative, import.meta.url) to find files next to the module, regardless of the page URL.
  • Host extras. Node.js adds import.meta.dirname and import.meta.filename (recent versions), and import.meta.resolve() exists in browsers and Node.js to resolve a module specifier. Bundlers add their own (import.meta.env, import.meta.hot); these are not standard.
  • Not a global. Each module has its own import.meta; it cannot be passed around as a "current module" pointer except by value.
  • Differs from __filename. CommonJS variables __filename and __dirname do not exist in ES modules.
  • Use in this sandbox. The editor on this site runs the code in an isolated preview, so import.meta.url may be an unusual or opaque value; do not rely on it for fetching files.
  • Static analysis. Bundlers recognize patterns like new URL("./x.png", import.meta.url) and rewrite them to asset paths.

Browser support

Supported in current browsers for module scripts. Properties beyond url, such as resolve(), dirname and filename, vary by environment and version, so check a compatibility table.