node.js

"'cb' argument must be of type function" error, invoking fs.writeFile()


I wrote this code:

var fs = require('fs');

var data = {
    name:'Bob'
}

and I get this error:

fs.writeFile('data.json',data) "the 'cb' argument must be of type function. Received undefined"

How do I fix it?


Solution

  • You're using the asynchronous callback-style fs.writeFile API without a callback (cb for short).

    In addition, you're not encoding your data to JSON before attempting to write it, so that too would fail.

    Either: