CommonJS files running in Node.js have access to two very helpful variables:
__dirname
- the directory in which the current file lives.__filename
- the full path to the current file.
In ECMAScript modules, however, these are no longer available by default. Fortunately, you can recreate them yourself to get the same information that to the import.meta.url
property:
import { fileURLToPath } from "node:url";
import path from "node:path";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
The import.meta.url
property is a file URL, not a file path, so you first have to convert it into a file path. After that, you just need to use path.dirname()
to pull off the directory.