Our system has (evidently) too many open files:
Error: EMFILE: too many open files
Should we explicitly close files after:
import * as fs from 'fs';
for (const filename in filenames) {
const text = fs.readFileSync(filename).toString();
// explictly close the file? how?
}
It seems that canonical answers (like this one) don't mention it.
Here is a solution that wraps the
readFileSyncwithopenSyncandcloseSync:It opens + reads + closes 100,000 files in a loops without any problem: