How to create an empty file in Node.js?

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.