xxxxxxxxxx
// Import dirname from path module and call it in path.resolve or path.join method and pass "./" as argument
import path, {dirname} from 'path';
const myFilePath = path.resolve(dirname('./'), "src", "index.js");
xxxxxxxxxx
// Because you're using ES module, and __dirname isn't a part of ESM.
// this code will help
import { fileURLToPath } from 'url';
import { dirname } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
xxxxxxxxxx
const {pathname: root} = new URL('../src', import.meta.url)
// no necesitar más el path.join luego lo que hacemos es acceder a la propiedad pathname del objeto URL que hemos creado.