How will you write a file using Node?


Following is the syntax of one of the methods to write into a file:

fs.writeFile(filename, data[, options], callback)

This method will over-write the file if file already exists. If you want to write into an existing file then you should use another method available.


Here is the description of the parameters used:

  • path - This is string having file name including path.

  • data - This is the String or Buffer to be written into the file.

  • options - The third parameter is an object which will hold {encoding, mode, flag}. By default encoding is utf8, mode is octal value 0666 and flag is 'w'

  • callback - This is the callback function which gets a single parameter err and used to to return error in case of any writing error.


© 2017