Categories
JavaScript Answers

How to create an empty file in Node.js?

Spread the love

Sometimes, we want to create an empty file in Node.js.

In this article, we’ll look at how to create an empty file in Node.js.

How to create an empty file in Node.js?

To create an empty file in Node.js, we can call the fs.openSync method.

For instance, we write

fs.closeSync(fs.openSync(filePath, 'w'));

to call fs.openSync with the filePath of the file to write to and the 'w' (write) permission.

Then we close the returned file handle with fs.closeSync.

Conclusion

To create an empty file in Node.js, we can call the fs.openSync method.

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 *