Rahul Iyer
Rahul Iyer

Reputation: 21025

How to avoid EMFILE, open error?

I get the error:

EMFILE, open <filename>

I don't think this is the same as the situation when there are too many files open.

When I run ulimit, the output is: unlimited , so I don't think that is the problem. I create many subdirectories, and when the subdirectories are creates, I call fs.exists to check that the directory actually exists before doing fs.writeFile

What is the problem ?

EDIT: ulimit -n outputs 1024

Upvotes: 2

Views: 3012

Answers (2)

Plaute
Plaute

Reputation: 4899

You should not use writeFile or even appendFile, both of them open file to each append.

Using a stream is far better. You can manage your own file handle and close it when you want, or not :)

var stream = fs.createWriteStream("append.txt", {flags:'a'});
stream.write("data");
stream.end;

Upvotes: 1

Rahat Mahbub
Rahat Mahbub

Reputation: 2987

You should try graceful-fs: https://github.com/isaacs/node-graceful-fs

As the description says, it Queues up open and readdir calls, and retries them once something closes if there is an EMFILE error from too many file descriptors.

Here's a nice explanation: http://blog.izs.me/post/56827866110/wtf-is-emfile-and-why-does-it-happen-to-me

Upvotes: 2

Related Questions