Fs.readFileSync - Error: EMFILE, too many open files D: \ Workspace \ some.json '

I have searched here for a long time but got no answer, I just want to read 4000 json files in a loop and do something later,

try {
  data = JSON.parse(fs.readFileSync(file));
} catch (err) {
  console.error(err);
  next();
  return;
}

      

It's such a simple problem, why can't I find the answer? I tried graceful-fs

, still getting the same problem.

Any suggestion? Many thanks!

+3


source to share


3 answers


Finally, use readFile and open openFileSync, which looks like "sync", but why isn't it?



    fs.readFile(file, function read(err, data) {})

      

+2


source


I had the same problem when I browsed through folders and downloaded files. I was only able to solve it by using queues and viewing them from the queue. I ended up going with an async library



+1


source


You can use the following parameter to avoid this problem ....

var Filequeue = require ('filequeue');

var fq = new Filequeue (200); // maximum number of files to open at once

fq.readdir ('/ path / to / files /', function (err, files) {if (err) {throw err; enter code here } files.forEach(function(file) { fq.readFile('/path/to/files/' + file, function(err, data) {

enter code here `// do something other than crash} Make sure you have installed filequeue npm here ...

0


source







All Articles