Skip to content

import.meta — where the current module lives

Level: 201 · for anyone who needs to know where the running module is

Stub — an outline, not a lesson. There is no runnable example behind this page yet, so nothing on it has been through the check that backs every other claim in this library. The questions below are what the finished page has to answer.

One line: import.meta.url is a file: URL, not a path — a folder named meta dir comes back as meta%20dir — and Node adds filename, dirname, resolve() and main to the same object, which a .cjs file cannot even mention.

Keywords: import.meta, import.meta.url, import.meta.resolve, import.meta.main

What the finished page will answer

  • What do import.meta.url and import.meta.filename print for a module in a folder whose name has a space?
  • Which keys does import.meta have in Node, and what does each hold for the running file?
  • When is import.meta.main true, and what was the CommonJS way to ask the same question?
  • Does import.meta.resolve("./missing.mjs") check that the file exists?
  • What happens when a .cjs file mentions import.meta?

Examples it will need

  • import_meta_fields_sh.sh — every key of import.meta for a module in a folder named with a space, run directly and then imported by another module

See also

  • Paths and file URLs — turning import.meta.url into a path, and the dirname that replaces __dirname
  • ES modules and CommonJS — the CommonJS variables that import.meta stands in for
  • process — process.argv, the other record of what is running

Sources to start from