What the scenario call fs.close is necessary

node.jsFile Io

node.js Problem Overview


I can't find more about fs.close explain in nodejs API. I want to know what the scenario call fs.close is necessary. for example:

var fs =  require('fs');
fs.writeFile("/home/a.tex","abc"); or like fs.appendFile("/home/a.tex","close")
fs.close(); //is it necessary?

Are there any effects if i don't call fs.close?

Any help is appreciated.

node.js Solutions


Solution 1 - node.js

You don't need to use fs.close after fs.readFile, fs.writeFile, or fs.appendFile as they don't return a fd (file descriptor). Those open the file, operate on it, and then close it for you.

The streams returned by fs.createReadStream and fs.createWriteStream close after the stream ends but may be closed early. If you have paused a stream, you must call close on the stream to close the fd or resume the stream and let it end after emitting all its data.

But if you call fs.open or any of the others that give a fd, you must eventually fs.close the fd that you are given.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionL.TView Question on Stackoverflow
Solution 1 - node.jsDan D.View Answer on Stackoverflow