Node.js fs-extra outputFile() Function Last Updated : 07 Oct, 2021 Summarize Comments Improve Suggest changes Share Like Article Like 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. encoding: It is a string that defines the encoding of the file. By default, the value is utf-8. mode: It is an integer value that defines the file mode. By default, the value is 0o666. 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. signal: AbortSignal allows aborting an in-progress outputFile. Return value: It does not return anything. Follow the steps to implement the function: The module can be installed by using the following command: npm install fs-extra After the installation of the module you can check the version of the installed module by using this command: npm ls fs-extra 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'); 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 Comment More infoAdvertise with us Next Article Node.js fs-extra move() Function P pritishnagpal Follow Improve Article Tags : Node.js Technical Scripter 2020 NodeJS-fs-extra Similar Reads Node.js fs-extra outputJson() Function The outputJson() function writes an object to the JSON file. If the user wants to write data onto a file that doesn't exist it will be created by the function itself. outputJSON() can also be used in place of outputJson(). Syntax: fs.outputJson(file,object,options,callback) or fs.outputJSON(file,obj 3 min read Node.js fs-extra remove() Function the remove() function deletes the given file or directory. All the files inside a directory are deleted. If the given file or directory does not exist the function will do nothing. Syntax: fs.remove(path,callback) Parameters: This function accepts two parameters as mentioned above and described belo 1 min read Node.js fs-extra pathExists() Function The pathExists() tests whether the given file path exists or not. It uses the fs.access() under the hood. Syntax: fs.pathExists(file,callback) Parameters: This function accepts two parameters as mentioned above and described below: file: It is a string that contains the file path.callback: It will b 2 min read Node.js fs-extra move() Function The move() function moves a file or a directory from source to the destination specified by the user. If you want to move a file to a folder in which a file with the same name already exists, the function will overwrite the file if we have set the option of overwrite to true else it will throw an er 3 min read Node.js fs-extra emptyDir() Function The fs-extra is a module that adds file system methods that are not included in the native fs module. It also adds promises support to the fs method. Some file system methods are not included in the native fs module, therefore, they have to be installed separately if we need to use them but the fs-e 3 min read Node.js fs.extra ensureDir() Function The ensureDir() function make sure that the directory user is requesting exists. If the directory structure does not exist the function will create the structure itself. mkdirs() and mkdirp() are the other names of the function which means we can use them in place of ensureDir() and everything will 2 min read Like