The Wayback Machine - https://web.archive.org/web/20250115181012/https://www.geeksforgeeks.org/javascript-program-to-write-data-in-a-text-file/
Open In App

JavaScript Program to write data in a text File

Last Updated : 09 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

To write data to a text file using JavaScript in a Node.js environment, the built-in fs (File System) module provides efficient file operations. It supports reading, writing, appending, and deleting files, making it a powerful tool for handling file-related tasks in Node.js.

Used Function:

The writeFile() functions is used for writing operations. 

Pre-requisites:

Syntax:

writeFile( Path, Data, Callback)

Parameters:

  • Path: It takes in the relative path from the program to the text File. If the file is to be created in the same folder as that of the program, then give the name of the file only. If the file does not exist then a new file will be created automatically.
  • Data: This argument takes in data that needs to be written in the file.
  • Callback Function: It is the callback function that further has an argument (err). If the operation fails to write the data, an error shows the fault.

Example 1: This Node.js script uses the fs module to write the string “Learning how to write in a file.” into a file named Output.txt, handling errors if they occur.

javascript
// Requiring fs module in which
// writeFile function is defined.
const fs = require('fs')

// Data which will write in a file.
let data = "Learning how to write in a file."

// Write data in 'Output.txt' .
fs.writeFile('Output.txt', data, (err) => {

    // In case of a error throw err.
    if (err) throw err;
})

Output:

Learning how to write in a file.

Example 2: This Node.js script uses the fs module to write the string “Hello world.” into a file named Hello.txt, while handling potential errors with a callback function.

JavaScript
// Requiring fs module in which
// writeFile function is defined.
const fs = require('fs')

// Data which will write in a file.
let data = "Hello world."

// Write data in 'Hello.txt' .
fs.writeFile('Hello.txt', data, (err) => {

    // In case of a error throw err.
    if (err) throw err;
})

Output:

Hello world.

Example 3: This Node.js script uses the fs module’s appendFile method to add the string “\nThis is some additional text.” to the existing file Hello.txt, handling potential errors.

JavaScript
// Requiring fs module in which
// writeFile function is defined.
const fs = require('fs')

// Data which will be appended to the file.
let newData = "\nThis is some additional text."

// Append data to 'Hello.txt' .
fs.appendFile('Hello.txt', newData, (err) => {

    // In case of a error throw err.
    if (err) throw err;
})

Note: Above script can be run using the NodeJs interpreter in the terminal.

Conclusion

In conclusion, the fs module in Node.js provides a simple and efficient way to handle file operations like writing, reading, and appending data. With easy-to-use functions like writeFile() and appendFile(), managing files becomes straightforward and reliable.


Become an expert in solving problems with DSA JavaScript—the course designed to teach you Data Structures and Algorithms using JavaScript. Over the next 90 days, dive deep into key DSA concepts, learn to optimize your code, and gain hands-on experience solving challenging problems. Whether you're a beginner or looking to refine your JavaScript skills, this course will provide the knowledge you need to succeed.
Take on the Three 90 Challenge today! Complete 90% of the course within 90 days, and you’ll earn a 90% refund. This challenge is your chance to stay motivated, improve your skills, and get rewarded for your progress. Don’t wait—start your journey to DSA mastery with JavaScript today!


Next Article

Similar Reads

three90RightbarBannerImg