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.
const dataUrl = new URL("./data.json", import.meta.url);
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
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.urlis the module's full URL as a string (https://...in browsers,file:///...in Node.js ES modules). Combine it withnew URL(relative, import.meta.url)to find files next to the module, regardless of the page URL.- Host extras. Node.js adds
import.meta.dirnameandimport.meta.filename(recent versions), andimport.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__filenameand__dirnamedo not exist in ES modules. - Use in this sandbox. The editor on this site runs the code in an isolated preview, so
import.meta.urlmay 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.