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().
console.log(import.meta.url); // file:///.../main.mjs console.log(import.meta.dirname); // absolute folder path
Try it
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.
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
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:
filenameanddirnamein Node 20.11 and 21.2;resolve()is synchronous since Node 20. Older runtimes needfileURLToPath(import.meta.url). import.meta.urlis a URL string, not a path. Usenew URL('./x', import.meta.url)for siblings; mostfsfunctions accept URL objects directly.- Use
fileURLToPath()for tools that need a path string. Do not slice offfile://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,__filenameandrequire.resolve()there. In a file that must run both ways, prefer separate entry points. - Entry-point check:
import.meta.main(Node 24.2+) istruewhen this file was started directly, likerequire.main === modulein CommonJS. On older versions compareprocess.argv[1]withfileURLToPath(import.meta.url); symbolic links can make these differ. resolve()respects theexportsmap of packages and the--conditionsflag, so it shows where an import would actually land.