 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
outputFile() function in fs-extra - NodeJS
Introduction to Async outputFile()
This method is similar to write file of 'fs'. The only difference is it will create the parent directories, if not present. The argument passed should always be a file path, not a buffer or a file descriptor.
Syntax
outputFile(file, data[, options] [, callback])
Parameters
- file – String parameter which will contain name and location of the file. 
- data – The data can hold a string data, buffer stream or a Unit8 string array that is to be stored. 
-  options – The 'outputFile' function supports the following options − - encoding – Default 'utf8'. 
- mode – Default 0o666 
- signal – allown aborting an ongoing output file function 
 
- callback – This function will give a callback if any error occurs. 
Example
- Check that fs-extra is installed before proceeding; if not, install fs-exra. 
- You can use the following command to check whether fs-extra is installed or not. 
npm ls fs-extra
- Create a asyncOutputFile.js and copy-paste the following code snippet into that file. 
- Now, run the following command to run the following code snippet. 
node asyncOutputFile.js
Code Snippet −
const fs = require('fs-extra') const file = '/tmp/not/exist/file.txt' // Writing file with a callback: fs.outputFile(file, 'Welcome to Tutorials Point!', err => {    console.log(err) // => null    fs.readFile(file, 'utf8', (err, data) => {       if (err) return console.error(err)       console.log(data) // => Welcome to Tutorials Point!    }) }) // Writing file with Promises: fs.outputFile(file, 'Welcome to Tutorials Point!') .then(() => fs.readFile(file, 'utf8')) .then(data => {    console.log(data) // => Welcome to Tutorials Point! }) .catch(err => {    console.error(err) }) // Writing file async/await: async function asyncOutputFileExample (f) {    try {       await fs.outputFile(f, 'Welcome to Tutorials Point!')       const data = await fs.readFile(f, 'utf8')       console.log(data) // => Welcome to Tutorials Point!    } catch (err) {       console.error(err)    } } asyncOutputFileExample(file) Output
C:\Users\tutorialsPoint\> node asyncOutputFile.js null Welcome to Tutorials Point! Welcome to Tutorials Point! Welcome to Tutorials Point!
Introduction to outputFileSync()
This method is same as writeFileSync from 'fs'. The only difference is it will create the parent directories, if not present. The argument passed should always be a file path, not a buffer or a file descriptor.
Syntax
outputFileSync(file, data[, options])
Parameters
- file – This is a string paramter which will hold the location of the file. 
- data – The data can hold a string data, buffer stream or a Unit8 string array that is to be stored. 
-  options – The 'outputFile' function supports the following options − - encoding – Default 'utf8'. 
- mode – Default 0o666 
- flag – Different flag options for operations on files 
 
Example
- Check that fs-extra is installed before proceeding; if not, install fs-exra. 
- You can use the following command to check whether fs-extra is installed or not. 
npm ls fs-extra
- Create an outputFileSyncExample.js and copy-paste the following code snippet into that file. 
- Now, run the following command to run the following code snippet. 
node outputFileSyncExample.js
Code Snippet
const fs = require('fs-extra') const file = '/tmp/this/path/does/not/exist/file.txt' fs.outputFileSync(file, 'Welcome to Tutorials Point!') const data = fs.readFileSync(file, 'utf8') console.log(data) // => Welcome to Tutorials Point! Output
C:\Users\tutorialsPoint\> node outputFileSyncExample.js Welcome to Tutorials Point!
