Host-defined import.meta metadata
How it works
import.meta is a special expression available inside an ES module. It exposes an object populated by the host, not a fixed list of ECMAScript properties. import.meta.url commonly identifies the current module in browser and Node environments. Resolve a relative resource URL against this base using new URL(relative, import.meta.url); resolution does not itself read the resource.
Example 1
Save each file separately. Run with Node.js: main.mjs. This is a multi-file example, not a single editor snippet.
main.mjs
const url = new URL("./data.json", import.meta.url);console.log(import.meta.url.endsWith("/main.mjs"), url.pathname.endsWith("/data.json"));Expected output in the tested environment:
true trueSuffix checks avoid depending on the machine’s absolute directory.
Common mistake
import.meta.dirname is not a universal browser property. Check the host and version before using nonportable metadata.
Practice
Resolve ../assets/logo.svg from the displayed module and explain the difference between computing its URL and loading its contents.
Run this practice in the documented local environment. ByteMind module and asynchronous grader support is not yet verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Host-defined import.meta metadata — Knowledge Check
Reason about the concept and predict its behavior in the indicated environment.
Who supplies the properties of import.meta?
What does new URL(relative, import.meta.url) do?
Where is import.meta syntactically available?
What should portable code assume about host-specific metadata fields?
For Host-defined import.meta metadata, what is the console output of example 1 in Node.js 22.16.0?
// File: main.mjsconst url = new URL("./data.json", import.meta.url);console.log(import.meta.url.endsWith("/main.mjs"), url.pathname.endsWith("/data.json"));