__dirname

 

__dirname 

n'est pas reconnu lorsque l'on passe en version de module EJS. Voici deux solutions possibles.

🪛Redéfinir __dirname 

Dans test.mjs

import { dirname } from "path";
import { fileURLToPath } from "url";
import { readFile } from "fs/promises";

const path = "/hello.txt";
const __dirname = dirname(fileURLToPath(import.meta.url));
const data = await readFile(__dirname + path);

console.log(data);
console.log(data.toString());


🪛Oublier __dirname 

import { URL } from "node:url";
import { readFile } from "node:fs/promises";

const path = "./hello.txt";
const data = await readFile(new URL(path, import.meta.url));

console.log(data);
console.log(data.toString());