Categories
JavaScript Answers

How to create an empty file in Node.js?

Spread the love

To create an empty file in Node.js, we use the open method.

For instance, we write

const fs = require("fs");
fs.open(path, "wx", (err, fd) => {
  // handle error
  fs.close(fd, (err) => {
    // handle error
  });
});

to call open with the path to open the file at the path.

We create the file since we called it with the w permission.

And then we call close to close the file.

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 *