Write text into a file in JavaScript

// Import the built-in 'fs' (file system) module
const fs = require('fs');

// Text we want to write into the file
const content = "Hello! This text is written to a file using Node.js.";

// Write to a file called 'example.txt'
fs.writeFile('example.txt', content, (error) => {
  if (error) {
    // If an error happens, print it
    console.log("Error writing file:", error);
  } else {
    // If successful
    console.log("File written successfully!");
  }
});

Leave a Reply

Your email address will not be published. Required fields are marked *