title: Nodejs - fs.appendFileSync throws Error EBUSY
body: <p>I am trying to append lines to a file in nodejs. I have written the following peice of code .</p>
  
  <pre><code>/* Name :  test.js */
  /* globals require,__dirname */
  var fs = require("fs");
  for (var i = 0; i &lt; 100000; i++) {
      fs.appendFileSync(__dirname+'/out.txt', "line " + i + "\n");
  }
  </code></pre>
  
  <p>But when I run this program, I am getting the following error. </p>
  
  <pre><code>$ node test.js
  fs.js:584
    return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
                                                    ^
  
  Error: EBUSY: resource busy or locked, open 'd:\&lt;redacted&gt;\out.txt'
      at Error (native)
      at Object.fs.openSync (fs.js:584:18)
      at Object.fs.writeFileSync (fs.js:1224:33)
      at Object.fs.appendFileSync (fs.js:1283:6)
      at Object.&lt;anonymous&gt; (d:\&lt;redacted&gt;\test.js:3:8)
      at Module._compile (module.js:425:26)
      at Object.Module._extensions..js (module.js:432:10)
      at Module.load (module.js:356:32)
      at Function.Module._load (module.js:313:12)
      at Function.Module.runMain (module.js:457:10)
  </code></pre>
  
  <p>I have searched but could not find any reason or solution for this. Can someone please explain why this Error occurs and how to fix it ?</p>
  
  <p>When I try the following method it works but this is not very feasible for the problem I am attempting to solve. </p>
  
  <pre><code>/* globals require,__dirname */
  var fs = require("fs");
  var buffer = "";
  
  for (var i = 0; i &lt; 100000; i += 1) {
      var line =  'line ' + i + '\n';
      buffer += line;                 // keep the line in buffer    
  }
  
  fs.appendFileSync(__dirname+'/out.txt', buffer);   // now write the buffer
  </code></pre>
  
  <p>Thanks in advance. </p>