The Wayback Machine - https://web.archive.org/web/20250223184345/https://www.geeksforgeeks.org/node-js-fs-writefile-method/
Open In App

Node JS fs.writeFile() Method

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

The fs.writeFile() method is a built-in function in Node.js used to write data to a file. It allows you to create a new file or overwrite an existing file with specified content. This function is part of the fs (File System) module, which provides an extensive API for working with the file system in Node.js.

Syntax:

fs.writeFile( file, data, options, callback )

Parameters:

This method accepts four parameters as mentioned above and described below:

  • file: It is a string, Buffer, URL or file description integer that denotes the path of the file where it has to be written. Using a file descriptor will make it behave similarly to fs.write() method.
  • data: It is a string, Buffer, TypedArray or DataView that will be written to the file.
  • options: It is a string or object that can be used to specify optional parameters that will affect the output. It has three optional parameters:
    • encoding: It is a string value that specifies the encoding of the file. The default value is ‘utf8’.
    • mode: It is an integer value that specifies the file mode. The default value is 0o666.
    • flag: A string value specifies the flag used while writing to the file. The default value is ‘w’.
  • callback: It is the function that would be called when the method is executed.
    • err: It is an error that would be thrown if the operation fails.

Steps To Implement fs.writeFile() Method

Step 1: Create a node project using the following command.

mkdir nodejs
cd nodejs

Step 2: Initialize the project using the following command.

npm init -y

Project Structure

NodeProj

Project Structure

Example 1: Below examples illustrate the fs.writeFile() method in Node.js:

JavaScript
// Filename - index.js

// Import the filesystem module
const fs = require("fs");

let data = "This is a file containing a collection of books.";

fs.writeFile("books.txt", data, (err) => {
    if (err) console.log(err);
    else {
        console.log("File written successfully\n");
        console.log("The written file has the following contents:");
        console.log(fs.readFileSync("books.txt", "utf8"));
    }
});


Output

File written successfully
The written has the following contents:
This is a file containing a collection of books.

Example 2: Below examples illustrate the fs.writeFile() method in Node.js:

javascript
// Filename - index.js

// Import the filesystem module
const fs = require("fs");

let data = "This is a file containing a collection of movies.";

fs.writeFile(
    "movies.txt",
    data,
    {
        encoding: "utf8",
        flag: "w",
        mode: 0o666
    },
    (err) => {
        if (err) console.log(err);
        else {
            console.log("File written successfully\n");
            console.log("The written file has the following contents:");
            console.log(fs.readFileSync("movies.txt", "utf8"));
        }
    }
);


Output:

File written successfully
The written has the following contents:
This is a file containing a collection of movies.

Next Article

Similar Reads

three90RightbarBannerImg