Create an empty file in Node.js?

If you want to force the file to be empty then you want to use the 'w' flag instead:

var fd = fs.openSync(filepath, 'w');

That will truncate the file if it exists and create it if it doesn't.

Wrap it in an fs.closeSync call if you don't need the file descriptor it returns.

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

Here's the async way, using "wx" so it fails on existing files.

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