Load adjacent resources
Read configuration, templates, schemas, or other assets located beside an ES module.
Node.js · Pipe
import.meta.url is the absolute file URL of the current ES module. Use it as the base for resolving nearby resources with URL-aware Node.js APIs.
In an ES module, import.meta.url is the absolute file URL identifying that module. Use it as the base for new URL() when locating nearby resources, then convert or pass the resulting URL to Node.js APIs that accept URLs.
import.meta.urlA string containing the absolute file URL of the current ES module.
import { readFile } from 'node:fs/promises';
const data = JSON.parse(
await readFile(new URL('./config.json', import.meta.url), 'utf8'),
);
console.log(data);Run this as an ES module with a neighboring config.json file; the URL is resolved relative to the current module.
Read configuration, templates, schemas, or other assets located beside an ES module.
Build a URL for a relative import or resource without manually concatenating filesystem paths.
Pass a module-relative URL to filesystem APIs that accept URL objects.
import.meta.url is a string containing the absolute file URL of the current ES module. It is useful as a stable base for resolving resources relative to that module.
Follow-up: How would you read a sibling file? Use new URL('./file', import.meta.url), then pass that URL to an fs API such as readFile().
Construct a URL relative to import.meta.url, for example new URL('./data.proto', import.meta.url). Node.js fs APIs can consume the resulting URL directly.
Follow-up: Why prefer URL resolution? It follows module URL semantics and avoids manually concatenating filesystem paths.
The value is available only in ES modules, and the documented property is a string representing the module's absolute file URL. Modules that do not use the file protocol do not provide the related local-file metadata.
Follow-up: What related API can replace require.resolve for module resolution? import.meta.resolve provides that purpose in supported ES module contexts.
import.metaimport.meta.dirnameimport.meta.filenameimport.meta.mainimport.meta.resolveurl.fileURLToPath