Open In App

Node.js fs-extra outputFile() Function

Improve
Improve
Like Article
Like
Save
Share
Report

The outputFile() function writes the data onto the given file. It is similar to the writeFile() function except that if the file onto which the data has to be written does not exist, it will be created by the function itself. Even if the file is located in a directory that does not exist, it will be created by the function itself.

Syntax:

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

Parameters: This function accepts four parameters as mentioned above and described below.

  • file: It is a string that defines the path of the file where it has to be written.
  • data: It is a string, Buffer, TypedArray, or DataView that will be written to the file.
  • options: It is a string or an object that is used to specify optional parameters. The available options are:
  • callback: It will be called after the task is completed by the function. It will either result in an error or success. Promises can also be used in place of the callback function.
  1. encoding: It is a string that defines the encoding of the file. By default, the value is utf-8.

  2. mode: It is an integer value that defines the file mode. By default, the value is 0o666.

  3. flag: It is a string value that defines the flag used while writing to the file. By default, the value is ‘w’. You can check the flags here.

  4. signal: AbortSignal allows aborting an in-progress outputFile.

Return value: It does not return anything.

Follow the steps to implement the function:

  1. The module can be installed by using the following command:

    npm install fs-extra
  2. After the installation of the module you can check the version of the installed module by using this command:

    npm ls fs-extra

  3. Create a file with the name index.js and require the fs-extra module in the file using the following command

    const fs = require('fs-extra');
  4. To run the file write the following command in the terminal:

    node index.js

The project structure will look like this:

Example 1:

index.js




// Requiring module
import  fs from "fs-extra";
  
// file already exist
// so data will be written
// onto the file
const file = "file.txt";
  
// This data will be
// written onto file
const data = "This is geeksforgeeks";
  
// Function call
// Using callback function
fs.outputFile(file, data, (err) => {
  if (err) return console.log(err);
  console.log("Data successfully written onto the file");
  console.log("Written data is: ");
  //   Reading data after writing on file
  console.log(fs.readFileSync(file, "utf-8"));
});


Output:

Example 2:

index.js




// Requiring module
import fs from "fs-extra";
  
// file and directory
//  does not  exist
// so both  will be created
// and data will be written
// onto the file
const file = "dir/file.txt";
  
// This data will be
// written onto file
const data = "This data will be written on file which doesn't exist";
  
// Additional options
const options = {
  encoding: "utf-8",
  flag: "w",
  mode: 0o666,
};
  
// Function call
// Using Promises
fs.outputFile(file, data, options)
  .then(() => {
    console.log("File Written successfully");
    console.log("Content of file: ");
    console.log(fs.readFileSync(file, "utf-8"));
  })
  .catch((e) => console.log(e));


Output:

Reference: https://github.com/jprichardson/node-fs-extra/blob/HEAD/docs/outputFile.md



Last Updated : 07 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads