To fix writeFile no such file or directory error with Node, we call the mkdirp function to create the directory if it doesn’t exist.
For instance, we write
const mkdirp = require("mkdirp");
const fs = require("fs");
const writeFile = async (path, content) => {
await mkdirp(path);
fs.writeFileSync(path, content);
};
to define the writeFile function.
In it, we call mkdirp to create the directory at the path if it doesn’t exist.
Then we call writeFileSync to write the file to the path.