Writing to File in Node.js

Introduction

Writing some content to a file is a very common programming task. If you are building an application, then one of the important things is to write the content to a file. Node.js core API provides methods to make it easier for us. Node.js fs module provides API to do file operations.

One simplest way to write content to a file is with fs.writeFile() as following.

Syntax:

fs.writeFile(file, data[, options], callback)
const fs = require('fs')

const content = 'Some Content'

fs.writeFile('/tmp/foo.txt', content, err => {
  if (err) {
    console.error(err)
    return
  }
  console.log("File written successfully");
  //file written successfully
})

The above code snippet uses asynchronous API to write the content to a file. Alternatively, you can use the synchronous version fs.writeFileSync() as follows.

Syntax:

fs.writeFileSync(file, data[, options])
const fs = require('fs')

const content = 'Some Content!'

try {
  const data = fs.writeFileSync('/tmp/foo.txt', content)
  //file written successfully
} catch (err) {
  console.error(err)
}

Both fs.writeFile() and fs.writeFileSync() write the full content of the file. By default, these APIs will replace the contents of the file if it does already exist. If you want to change the behavior then you have to provide flags in the options arguments.

fs.writeFile('/tmp/foo.txt', content, { flag: 'a+' }, err => {})

Some common flags to be used while file writing is as follows:

  • r+ open the file for reading and writing
  • w+ open the file for reading and writing, positioning the stream at the beginning of the file. The file is created if it does not exist
  • a open the file for writing, positioning the stream at the end of the file. The file is created if it does not exist
  • a+ open the file for reading and writing, positioning the stream at the end of the file. The file is created if it does not exist

You can get list of supported flags here.

Do not use synchronous API fs.readFileSync(). Synchronous API always blocks the Node event loop. As Node.js runs in a single threaded environment. It is advisable to use asynchronous APIs in production environment.

#node js file write example #node js file write append #node js file write stream #node js file write sync #node.js file write json #node js write file create directory #node js write file to directory #node js write file line by line #node js file write #node js write file async #node js write file async await #node js create file and write to it #node js file read and write #node js open file and write #node js fs write file array #node js write a file #node.js write file base64 #node js write file buffer #node js write file binary #node js write blob to file #node js fs write binary file #node js write bytes to file #node js fs write buffer to file #node.js write file content #node js file content type #node js write file to current directory #node js write csv file #node js write config file #node.js write css file #node js write console to file #node.js write file permission denied #node js write file to disk #node js write data to file #node js write data to json file #node.js debug write to file #node js write file encoding #node js write file sync example #node js write excel file #node js write file if not exists #node js write empty file #node js read write excel file #node js write error to file #node js write file from buffer #node.js write file fs #node js write file to folder #node js open file for writing #node js fs write file sync #node js fs write file append #node js fs write file async #node js fs write file new line #node js write html file #node js fs write html file #node js write http response to file #node.js how to write to file #node.js write file in memory #node js write file in loop #node js write in file #node js write ini file #node js write image file #node js write into json file #node js write in html file #node js write json object to file #node js write json array to file #node.js write json to text file #node js write js file #node js write json data to file #node js write pretty json to file #node js write large json to file #node.js to write file #node js write file new line #node js write log file #node js read and write file line by line #node js write large file #node js winston write log file #node js file mime type #node js write multiple files #node js write multiple lines to file #node js fs module write file #node js file-type module #node js write new file #node js write file overwrite #node js write file without overwrite #node js write object to file #node.js write to file on server #node js write object to json file #node js write to end of file #write on file node js #node js write file promise #node.js write file permission #node js write file path #node js write file position #node js fs write file path #node js write pdf file #node.js write properties file #node js write-file-queue #node js file read write #node js write file to response #node js write request to file #node js read write json file #node.js read write xml file #node.js readline write to file #node js write file sync append #node js write file synchronously #node.js write file string #node js write file using stream #node js fs write file stream #node.js write file to s3 #node.js write file typescript #node.js write file to server #node js write to file #node js write file utf 8 #node js write uint8array to file #write file using node js #node.js write variable to file #node.js write wav file #node js write to file windows #js write file without node #write file with node js #node js write xml file #node js write xlsx file #node js write yaml file #node js write zip file #node.js base64 write to file file write node js #write file node js append #creating files node js #write file in node js example #write file sync node js #write file stream node js #write file json node js #write file async node js #write file javascript node #node js write file async await #read and write file node js #node js asynchronous file write #create and write file node js #node js write a file #create folder and write file node js #write json to a file node js #write logs to a file in node js #node.js write file base64 #node js write file buffer #node js write file binary #node js write file line by line #node js write blob to file #node js fs write binary file #node js write bytes to file #csv write file node.js #node js write file create directory #node.js write file content #node.js cannot write file #node js write file to current directory #node js write config file #node.js write css file #write file in directory node js #node.js write file permission denied #node js write file to disk #node.js debug write to file #node js write data to file #write data into file node js #node js write file encoding #node.js excel file write #node js write file sync example #node js write empty file #node js write file if not exists #node js read write json file example #node js read write excel file #fs write file node js #node js fs write file sync #node js fs write file append #node js fs write file async #node js write file from buffer #node js fs write file new line #node js fs write file overwrite #node js fs write file path #how to write file node js #node js write html file #node js fs write html file #file write in node js #write file sync in node js #node.js write file in memory #file read write in node js #node js write file in loop #node js write js file #write json file using node js #javascript write to file node js #node js write json to file #node js write file new line #node js write log file #node js write large file #node js read and write file line by line #node js write to local file #node js write array to file line by line #node js fs module write file #node js write new file #node js write file overwrite #node write js object to file #write on file node js #node js open file and write #node js write file without overwrite #node.js write to file on server #node js write file promise #node.js write file permission #node js pipe write file #node js write file path #node js write file position #node js write pdf file #node.js write properties file #node js write-file-queue #read write file node js #node js read write json file #node.js read write xml file #node js write file to response #node.js readline write to file #node js read and write text file #node js write file sync append #node js write file synchronously #node.js write file string #node js write file using stream #node js fs write file stream #node.js write file to s3 #node write js to file #node js write file to directory

, , , , ,

Related posts

Latest posts

Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Please disable your adblocker or whitelist this site!

How to whitelist website on AdBlocker?

How to whitelist website on AdBlocker?

  1. 1 Click on the AdBlock Plus icon on the top right corner of your browser
  2. 2 Click on "Enabled on this site" from the AdBlock Plus option
  3. 3 Refresh the page and start browsing the site