When using CommonJS modules in Nodejs the following two variables are alwasy available as globals:
__filename
- absolute path to the currently executing file__dirname
- the absolute path to the directory that the currently executing file is in
More and more of Nodejs modules are moving to EcmaScript modules and in these cases, the above two no longer exists. Here is how to get the equivalent:
import {fileURLToPath} from 'node:url';
import path from 'node:path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(fileURLToPath(import.meta.url));
However, please do read the additional information here where the above originated from.