import.meta
Metadata object for the current module.
import.meta exposes metadata about the current module, including its URL in the browser.
console.log(import.meta.url);
const assetUrl = new URL("./icon.svg", import.meta.url);
Modules & Code Organization
In browser modules, import.meta.url contains the URL of the current module file.
A common pattern is new URL("./asset.ext", import.meta.url), which creates an asset URL relative to the module file.
Bundlers may add additional import.meta features, but those are tool-specific and should not be confused with browser-standard behavior.
Metadata object for the current module.
URL of the current module in the browser.
new URL can resolve paths relative to the module.
Bundlers may add extra properties.
Examples
const iconUrl = new URL("./icons/check.svg", import.meta.url);
icon.src = iconUrl.href;
icon.src = "../../assets/icons/check.svg"; // This can break when the module moves.
Code patterns
These examples focus on imports, exports, module boundaries, on-demand loading and file structure you can use in real projects.
Useful for debugging and relative paths.
console.log(import.meta.url);
Resolve relative to the module file.
const imageUrl = new URL("./image.png", import.meta.url);
URL objects can become strings.
image.src = imageUrl.href;
Not every environment has the same metadata.
if ("env" in import.meta) {
console.log("tool-specific metadata");
}
Rules that matter
A module system only helps when files have clear jobs, small public APIs and predictable import paths.
It travels with the module file.
Browser, Node and bundlers may differ.
import.meta.env is not a browser standard.
It avoids brittle string concatenation.
Choose a project convention.
Only expose URLs meant for the browser.
Production thinking
Module-relative URLs make code easier to move without breaking asset paths.
Module-resolved assets such as icons still need accessible names or hidden semantics where appropriate.
Production bundlers may rewrite asset URLs, so use supported patterns for the chosen toolchain.
Broken asset paths can hurt visual quality and trust, especially for icons and media in key UI.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
If you can explain what a file exports, what it imports and why it exists, the module structure is doing its job.