JavaScript import.meta

Beginner
5 min read
Updated: Nov 2026
3 examples

Definition and Usage

import.meta is an object with context about the current module. In browsers and Node, import.meta.url is the full URL of the module file.

JavaScript Syntax

JavaScript
console.log(typeof import.meta === "object" || "demo");
console.log("url" in (import.meta || {}) || "use in modules");
Try it Editor

How It Works

  1. Available in modules. Not in classic scripts.
  2. url. Common field for resolving assets.

Examples

Example 1 — Resolve assets

Build URLs relative to the module.

JavaScript
console.log("new URL('./data.json', import.meta.url)");
Try it Editor

How It Works

  1. Pattern for nearby files.

Example 2 — Host fields vary

Bundlers may add import.meta.env.

JavaScript
console.log("check your runtime docs");
Try it Editor

How It Works

  1. Not all fields are standard.

Example 3 — Classic scripts

import.meta is a syntax error outside modules.

JavaScript
console.log("use type=module");
Try it Editor

How It Works

  1. Modules required.

Frequently Asked Questions

The object exists per module; treat standard fields as read-only host data.

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful