Node.js fs.writeFile() Method - GeeksforGeeks

11 Oct, 2021
The fs.writeFile() method is used to asynchronously write the specified data to a file. By default, the file would be replaced if it exists. The ‘options’ parameter can be used to modify the functionality of the method.
Syntax:
fs.writeFile( file, data, options, callback )
Parameters: This method accept 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 similar to fs.write() method.
data: It is a string, Buffer, TypedArray or DataView that will be written to the file.
options: It is an string or object that can be used to specify optional parameters that will affect the output. It has three optional parameter: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: It is a string value that specifies the flag used while writing to the file. The default value is ‘w’.
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: It is a string value that 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.
err: It is an error that would be thrown if the operation fails.
Below examples illustrate the fs.writeFile() method in Node.js:
Example 1:
// Node.js program to demonstrate the// fs.writeFile() method  // Import the filesystem moduleconst 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 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:
// Node.js program to demonstrate the// fs.writeFile() method  // Import the filesystem moduleconst 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 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.
Reference: https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback
Node.js-fs-module
Picked
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to install the previous version of node.js and npm ?
Difference between promise and async await in Node.js
How to use an ES6 import in Node.js?
How to read and write Excel file in Node.js ?
Express.js res.render() Function
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Difference between var, let and const keywords in JavaScript