SyntaxStudy
Sign Up
JavaScript import.meta in Modules
JavaScript Advanced 4 min read

import.meta in Modules

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.

Example
// 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");
}
Pro Tip

Use new URL("./file", import.meta.url) to resolve paths relative to the current module — works both in browsers and Node.