How do I use Node to take a JS object (e.g. var obj = {a: 1, b: 2}
) and create a file that contains that object?
For example, the following works:
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}
})
But this doesn't work:
var obj = {a: 1, b: 2}
fs.writeFile('./temp.js', obj, function(err) {
if(err) console.log(err)
// creates a file containing: [object Object]
})
Update: JSON.stringify()
will create a JSON object (i.e. {"a":1,"b":2}
) and not a Javascript object (i.e. {a:1,b:2}
)