import.meta
import.meta is an object with metadata about the current module. In browsers, import.meta.url is the module's URL. In bundlers, import.meta.env exposes environment variables.
import.meta is an object with metadata about the current module. In browsers, import.meta.url is the module's URL. In bundlers, import.meta.env exposes environment variables.
// Get current module URL (browser)
console.log(import.meta.url);
// → "https://example.com/js/utils.js"
// Load a file relative to the current module
const response = await fetch(new URL("./data.json", import.meta.url));
const data = await response.json();
// Vite/webpack environment variables
if (import.meta.env.DEV) {
console.log("Development mode");
}
Use new URL("./file", import.meta.url) to resolve paths relative to the current module — works both in browsers and Node.
More in JavaScript