Home > Articles > Web Development

  • Print
  • + Share This
Like this article? We recommend

Using writeFile()

To write to a file, use the writeFile() method. An example appears below:

var fs = require("fs");
var path = "c:\\Temp\\Test.txt";
var data = "Hello from the Node writeFile method!";

fs.writeFile(path, data, function(error) {
     if (error) {
       console.error("write error:  " + error.message);
     } else {
       console.log("Successful Write to " + path);

Although the code above will replace whatever text is within the file, you can use the appendFile() method in place of the writeFile() method to append the data instead of replacing it.

  • + Share This
  • 🔖 Save To Your Account