{"id":4472,"date":"2016-06-17T02:31:25","date_gmt":"2016-06-17T02:31:25","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=4472"},"modified":"2016-06-17T02:31:25","modified_gmt":"2016-06-17T02:31:25","slug":"fixing-node-error-emfile-many-open-files","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/fixing-node-error-emfile-many-open-files\/","title":{"rendered":"Fixing node error &#8220;EMFILE: too many open files&#8221;"},"content":{"rendered":"<p>I&#8217;ve found that sometimes when  I write scripts in Node, I get errors like this:<\/p>\n<pre>\nLoading info: Error: EMFILE: too many open files, \nopen 'D:\\projects\\image-annotation\\data\\youtube\\videos\\ZX8MBBohX3s.info.json'\n<\/pre>\n<p>Typically this is caused by invalid uses of &#8220;readFile&#8221; or &#8220;writeFile&#8221;, like so:<\/p>\n<pre lang=\"javascript\">\nwriteFile(\"json\/1\/\" + itemNumber + \".json\", \n  JSON.stringify(result, null, 2),            \n  (err) => {\n    if(err) {\n     return console.log(err);\n    }\n});\n<\/pre>\n<p>The reason that this doesn&#8217;t work is that the call is asynchronous &#8211; you&#8217;re supposed to provide a callback for success, and close the file there. <\/p>\n<p>If you don&#8217;t want to do this, you can easily fix this by using the &#8220;sync&#8221; methods:<\/p>\n<pre lang=\"javascript\">\nwriteFileSync(\"json\/1\/\" + itemNumber + \".json\", \n     JSON.stringify(result, null, 2));\n<\/pre>\n<p>In this case you don&#8217;t need the error handler, because errors will be thrown.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Fixing a &#8220;too many open files&#8221; error in Node<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[302],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/4472"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=4472"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/4472\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=4472"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=4472"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=4472"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}