What else could you get from openFileSync besides the file descriptor?
var fs = require('fs') var path = require('path') var fd = fs.openSync(path.join(process.cwd(), 'log.txt'), 'a') fs.writeSync(fd, 'contents to append') setTimeout(function () { console.log('closing file now') fs.closeSync(fd) }, 10000)
Running lsof /path/to/log.txt when the node script above is executed and lsof /path/to/log.txt is lsof /path/to/log.txt again after the script is executed shows that the file is closing correctly
Said what are you trying to do by opening a file? Perhaps there is a better way, for example, streaming for your specific situation.
source share