title: write a javascript object to a file in node
body: <p>How do I use Node to take a JS object (e.g. <code>var obj = {a: 1, b: 2}</code>) and create a file that contains that object?</p>
  
  <p>For example, the following works:</p>
  
  <pre><code>var fs = require('fs')
  
  fs.writeFile('./temp.js', 'var obj = {a: 1, b: 2}', function(err) {
    if(err) console.log(err)
    // creates a file containing: var obj = {a: 1, b: 2}
  })
  </code></pre>
  
  <p>But this doesn't work:</p>
  
  <pre><code>var obj = {a: 1, b: 2}
  
  fs.writeFile('./temp.js', obj, function(err) {
    if(err) console.log(err)
    // creates a file containing: [object Object]
  })
  </code></pre>
  
  <p><strong>Update</strong>: <code>JSON.stringify()</code> will create a JSON object (i.e. <code>{"a":1,"b":2}</code>) and not a Javascript object (i.e. <code>{a:1,b:2}</code>)</p>