Working with Files: How to create an empty file in Node.js

Join the AI Workshop to learn more about AI and how it can be applied to web development. Next cohort February 1st, 2026

The AI-first Web Development BOOTCAMP cohort starts February 24th, 2026. 10 weeks of intensive training and hands-on projects.


The method fs.openSync() provided by the fs built-in module is the best way.

It returns a file descriptor:

const fs = require('fs')
const filePath = './.data/initialized'

const fd = fs.openSync(filePath, 'w')

the w flag makes sure the file is created if not existing, and if the file exists it overwrites it with a new file, overriding its content.

Use the a flag to avoid overwriting. The file is still created if not existing.

If you don’t need the file descriptor, you can wrap the call in a fs.closeSync() call, to close the file:

const fs = require('fs')
const filePath = './.data/initialized'

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

Lessons in this unit:

0: Introduction
1: How to check if a file exists in Node.js
2: ▶︎ How to create an empty file in Node.js
3: How to get the names of all the files in a folder in Node
4: How to get the last updated date of a file using Node.js
5: How to remove a file with Node.js
6: How to write a JSON object to file in Node.js
7: How to empty a folder in Node.js
8: Working with file descriptors in Node
9: Node File Paths
10: Node file stats
11: Working with folders in Node
12: How to use the Node.js fs module with async/await
13: How to get the current folder in Node
14: How to get the file extension in Node.js from the MIME type
15: How to get the file extension in Node.js
16: How to list files in a folder in Node
17: How to mass rename files in Node.js
18: Node.js get all files in a folder recursively
19: Incrementing multiple folders numbers at once using Node.js
20: Save some text to a file in Node.js