Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Node.js · Pipe

import.meta.url

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.

How it works

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.

Syntax

import.meta.url

Returns

A string containing the absolute file URL of the current ES module.

Key points

  • import.meta is the metadata object exposed to an ES module.
  • import.meta.url identifies the current module with an absolute file URL.
  • Its meaning follows the browser convention for the current module URL.
  • Use the value as the base for locating files next to the module.
  • Resolve a relative resource by constructing a URL against import.meta.url.

Common mistakes

  • This property is unavailable in CommonJS modules; use an ES module context.
  • Do not assume a local filesystem file URL when the module uses another protocol.

Example: Read a file relative to the current 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.

When to use it

Load adjacent resources

Read configuration, templates, schemas, or other assets located beside an ES module.

Resolve module-relative locations

Build a URL for a relative import or resource without manually concatenating filesystem paths.

Use URL-aware filesystem APIs

Pass a module-relative URL to filesystem APIs that accept URL objects.

Interview questions

QWhat does import.meta.url contain?

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().

QHow do you load a file relative to an ES module?

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.

QWhat context and protocol constraints apply?

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.meta
  • import.meta.dirname
  • import.meta.filename
  • import.meta.main
  • import.meta.resolve
  • url.fileURLToPath

Source: ECMAScript modules | Node.js v26.11.1 Documentation. Checked 2026-10-15.