Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท Globals and Console

Node.js import.meta

Reference for import.meta in Node.js ES modules: url, dirname, filename and resolve(), with examples of locating files next to your module.

import.meta is an object that holds information about the ES module that is currently running. It can only be used inside an ES module (a .mjs file, or a .js file in a package with "type": "module"). Using it in CommonJS is a syntax error.

Node adds these members: url, filename, dirname, resolve() and main. They replace the CommonJS values __filename, __dirname and require.resolve().

NODE
console.log(import.meta.url);      // file:///.../main.mjs
console.log(import.meta.dirname);  // absolute folder path

Try it

Node.js The four members

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Node.js Resolving paths and specifiers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Syntax

NODE
import.meta.url      // string
import.meta.filename // string
import.meta.dirname  // string
import.meta.resolve(specifier) // string
import.meta.main     // boolean
Member Description
url The absolute file: URL of the module, as a string.
filename Absolute path of the module file (same idea as __filename). Only for file: modules.
dirname Absolute path of the containing folder (same idea as __dirname). Only for file: modules.
main true if this module is the program's entry point (Node 24.2+).
resolve(specifier) Resolves a module specifier the way import would and returns the resulting URL string.

Returns: url, filename, dirname and resolve() give strings; main is a boolean.

Edge cases: for modules not loaded from disk (for example data: URLs), filename and dirname are undefined. resolve() throws if a bare package cannot be found, and it does not check that a relative file exists.

Usage notes

  • Added in: filename and dirname in Node 20.11 and 21.2; resolve() is synchronous since Node 20. Older runtimes need fileURLToPath(import.meta.url).
  • import.meta.url is a URL string, not a path. Use new URL('./x', import.meta.url) for siblings; most fs functions accept URL objects directly.
  • Use fileURLToPath() for tools that need a path string. Do not slice off file:// by hand: it breaks on Windows drive letters and percent-encoded characters.
  • It is per module. Each file has its own import.meta; it is not shared and cannot be passed around as a global.
  • Not available in CommonJS. Use __dirname, __filename and require.resolve() there. In a file that must run both ways, prefer separate entry points.
  • Entry-point check: import.meta.main (Node 24.2+) is true when this file was started directly, like require.main === module in CommonJS. On older versions compare process.argv[1] with fileURLToPath(import.meta.url); symbolic links can make these differ.
  • resolve() respects the exports map of packages and the --conditions flag, so it shows where an import would actually land.