Join the AI Workshop and learn to build real-world apps with AI. A hands-on, practical program to level up your skills.
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 creates the file if it doesn’t exist, and if the file exists it overwrites it (clearing its content).
Use the a flag to avoid overwriting. The file is still created if it does not exist.
If you do not need the file descriptor, wrap the call in fs.closeSync() to close the file:
const fs = require('fs')
const filePath = './.data/initialized'
fs.closeSync(fs.openSync(filePath, 'w'))