How are you loading the file? According to this issue, the problem arises if you load it as an ECMAScript module which do not contain __dirname.
https://github.com/nodejs/help/issues/2907#issuecomment-671782092
Following the documentation below you may be able to resolve the issue:
https://nodejs.org/api/esm.html#esm_no_require_exports_module_exports_filename_dirname
import { fileURLToPath } from 'url';
import { dirname } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);