Sub topic 14
Module Metadata with import.meta
This chapter assumes you know how module specifiers resolve and how to run ES modules in the browser and in Node.js. You should also be comfortable with the URL class and async/await.
What import.meta Is
Every module gets its own import.meta object. The host, meaning the browser or Node.js, creates this object, and it describes the module that is currently running. The object exists only inside ES modules. If you write import.meta in a classic script or a CommonJS file, you get a SyntaxError.
The property you can count on in every host is import.meta.url. It holds the module's absolute URL. In a browser it looks like https://example.com/js/app.js. In Node.js it is a file:// URL.
| Property | Browser | Node.js | Value |
|---|---|---|---|
| `import.meta.url` | Yes | Yes | Absolute URL of this module |
| `import.meta.resolve(specifier)` | Yes | Yes (20.6+) | Absolute URL string the specifier would resolve to |
| `import.meta.filename` | No | Yes (20.11+) | File system path of this module |
| `import.meta.dirname` | No | Yes (20.11+) | Directory containing this module |
Resolving Files Relative to the Module
A relative string passed to fetch() resolves against the page URL, not against the module's location. If you move the page or load the module from a different page, the request breaks. To avoid that, build the URL from import.meta.url:
// widgets/loader.js
export async function loadTemplate() {
const templateUrl = new URL('./template.html', import.meta.url);
const response = await fetch(templateUrl);
return response.text();
}import.meta.resolve() applies the same resolution rules that import uses, including import maps, and returns the result as a string without loading anything:
console.log(import.meta.resolve('./helper.js'));
// e.g. https://example.com/widgets/helper.jsReplacing __dirname in Node.js
CommonJS gives you __dirname and __filename. ES modules do not have them. Recent Node.js versions provide import.meta.dirname and import.meta.filename instead. On older versions you can compute the same values yourself:
// paths.mjs
import { fileURLToPath } from 'node:url';
import path from 'node:path';
const filename = fileURLToPath(import.meta.url);
const dirname = path.dirname(filename);
console.log(import.meta.url); // file:///home/me/project/paths.mjs
console.log(dirname); // /home/me/project
console.log(import.meta.dirname === dirname); // true on Node 20.11+Functions in node:fs accept URL objects directly, so readFile(new URL('./data.txt', import.meta.url)) works without any conversion.
Exercise
A module at /lib/icons/index.js runs fetch('./sprite.svg'), and the page that loads it is /app/index.html. Which URL is requested, and how do you fix the call so it requests /lib/icons/sprite.svg?
Show answer
The request goes to /app/sprite.svg because the relative string resolves against the page URL. Use fetch(new URL('./sprite.svg', import.meta.url)), which resolves against the module's own URL.
Exercise
Why does writing import.meta.url in a file that Node.js loads as CommonJS fail, and what is the CommonJS equivalent of import.meta.dirname?
Show answer
import.meta is valid syntax only inside ES modules, so a CommonJS file throws a SyntaxError. The CommonJS equivalent is __dirname.