Categories
JavaScript Answers

How to fix writeFile no such file or directory error with Node?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *