Reading a file in Node.js

Introduction

Reading a file into memory is a very common programming task. If you are building an application, then one of the important things is to read the content of 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 read file is with fs.readFile() as following.

const fs = require('fs')

fs.readFile('/etc/hosts', (err, data) => {
  if (err) {
    console.error(err)
    return
  }
  console.log(data)
})

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

const fs = require('fs')

try {
  const data = fs.readFileSync('/etc/hosts', 'utf8')
  console.log(data)
} catch (err) {
  console.error(err)
}

Both fs.readFile() and fs.readFileSync() read the full content of the file in memory before returning the data.

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.

#file reading in node js #read file in node js example #parse file in node.js #csv file read in node js #file read write in node js #json file read in node js #excel file read in node js #read file async in node js #file read and write in node js #reading a file in node js #reading a json file in node js #reading a text file in node js #read file node js buffer #read file node js line by line #read file in chunks node js #read file content in node js #read file data in node js #read files in directory node js #reading data from json file in node.js #read properties file in node js example #fs read file in node js #read file function in node js #reading html file in node js #read file name in node js #read content of file in node js #property file reader in node js #read file path in node js #reading properties file in node.js #read file node js sync #read file stream in node js #read file in server node.js #reading text file in node js #reading xlsx file in node js read file in node.js #read file in node js example #read json file in node js #read text file in node js #read html file in node js #read csv file in node js #read excel file in node js #read file async in node js #read file in node js #read a file in node js #read audio file in node js #read a json file in node js #read a csv file in node js #read and write file in node js #read a text file in node js #read an excel file in node js #read a file from s3 node js #fs read file in node js #read file node js buffer #read binary file in node js #read file node js line by line #read file from s3 bucket in node js #read file in chunks node js #read file content in node js #how to read css file in node js #read file data in node js #read file from directory in node js #read data from excel file in node js #read data from csv file in node js #read data from text file in node js #read json data from file in node js #node js read file in same directory #node js read file names in directory #read properties file in node js example #read external json file in node js #read and write excel file in node js #how to read .env file in node js #upload and read excel file in node js #read from file in node js #read file function in node js #read file from url in node js #read file from folder in node js #read file from s3 in node js #read json from file in node js #fs.readfile in node js #read file in node #read file using node js #read content of file in nodejs #how to read file in node js #how to read json file in node js #how to read text file in node js #how to read csv file in node js #how to read xlsx file in node js #how to read xml file in node js #read image file in node js #read js file in node js #read file javascript node js #read key value from properties file in node js #read log file in node js #read large file in node.js #read a file in nodejs #read a file using node js #read multiple file in node js #read file metadata node js #read file name in node js #how to read a file using node js #read content of file in node js #read file object node js #cannot read property ‘file’ of undefined in node js #how to read file content in node js #read properties file in node js #read pdf file in node js #read file path in node js #read pem file in node js #how to read pfx file in node js #how to read png file in node js #read the file in node js #read rtf file in node js #read file node js sync #read file stream in node js #read file in server node.js #read file using stream in node js #read file size node.js #how to read file synchronously in node js #read the json file in node js #read file nodejs typescript #read and write text file in node js #how to read html file in node js #read uploaded file in node js #read file in nodejs using fs #read write file in node js #read word file in node js #read file with node js #read and write csv file in node js #read and write json file in node js #read and write xlsx file in node js #read xlsx file in node js #read xml file in node js #read and write xml file in node js #read yaml file in node js

, , , ,

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