How to Open File in Node.js?
Introduction
Opening a file is a common programming technique. Every web application does a file open operation. Whenever a file read or file write operation executes eventually it does a file open call as well. Without file open operation, reading and writing to a file are not possible.
To open file in Node.js, We can use built-in fs module of Node.js. fs module
provides different APIs to open a file in Node. In this post, we will learn about all the ways to open a file in Node.js
Asynchronous operation using fs.open()
fs.open() is a simplest way to open a file in Node.js. It is a asynchronous API, which does not blocks the event loop of Node.js. Syntax of fs.open()
is as follows:
fs.open(path[, flags[, mode]], callback)
This method accept four parameters, described as follows:
- path: It holds the name of the file to read or the entire path if stored at other location.
- flags: The operation in which file has to be opened. Default flag is “r”
- mode: Sets the mode of file i.e. r-read, w-write, r+ -readwrite. It sets to default as readwrite. (0o666).
- callback: It is a callback function that is called after opening a file. It takes two parameters:
- err: If any error occurs.
- fd: Returns the file descriptor of the opened file. FD is an ineger value.
Following code snippet is basic example of file open in Node.js.
const fs = require('fs'); // Open file foo.txt in read mode fs.open('foo.txt', 'r', function (err, f) { console.log('File opened successfully !'); });
Synchronous operation using fs.openSync()
fs.openSync() is alternative way to open a file but it is a synchronous API which blocks the event loop until the file open operation is completed. Syntax of fs.openSync()
is as follows:
fs.openSync(path[, flags, mode])
This method accept three parameters, described as follows:
- path: It holds the name of the file to read or the entire path if stored at other location.
- flags: The operation in which file has to be opened. Default flag is “r”
- mode: Sets the mode of file i.e. r-read, w-write, r+ -readwrite. It sets to default as readwrite. (0o666).
Following code snippet uses fs.openSync()
const fs = require('fs'); const fd = fs.openSync("foo.txt", 'r'); // Prints output console.log(fd);
fs.openSync()
returns an integer value which denotes the file descriptor of opened file.
Following is the list of supported flag in fs module.
'a'
: Open file for appending. The file is created if it does not exist.'ax'
: Like'a'
but fails if the path exists.'a+'
: Open file for reading and appending. The file is created if it does not exist.'ax+'
: Like'a+'
but fails if the path exists.'as'
: Open file for appending in synchronous mode. The file is created if it does not exist.'as+'
: Open file for reading and appending in synchronous mode. The file is created if it does not exist.'r'
: Open file for reading. An exception occurs if the file does not exist.'r+'
: Open file for reading and writing. An exception occurs if the file does not exist.'rs+'
: Open file for reading and writing in synchronous mode. Instructs the operating system to bypass the local file system cache.This is primarily useful for opening files on NFS mounts as it allows skipping the potentially stale local cache. It has a very real impact on I/O performance so using this flag is not recommended unless it is needed.This doesn’t turnfs.open()
orfsPromises.open()
into a synchronous blocking call. If synchronous operation is desired, something likefs.openSync()
should be used.'w'
: Open file for writing. The file is created (if it does not exist) or truncated (if it exists).'wx'
: Like'w'
but fails if the path exists.'w+'
: Open file for reading and writing. The file is created (if it does not exist) or truncated (if it exists).'wx+'
: Like'w+'
but fails if the path exists.
Conclusion
It is recommended to use an asynchronous function instead of synchronous. As synchronous function blocks the event loop and should not be used in the production environment.Thanks for reading.
#Node.js | fs.open() Method #Node.js File System Module #How to read file in node.js #how to write file in node.js #nodejs file open #open a file in node.js # open a file in write mode node.js #nodejs file open #node.js file writing #node.js file opening #node.js file reading #file reading in node.js #node.js open a file for writing #node.js file open function #fs module file open #fs module file reading #file reading fs module #file module node.js #file system node.js explained #node.js file operations #nodejs file execute #node.js open a file #file open node.js #node.js file reading #node.js file writing #nodejs append to a file #append to a file nodejs #node.js writing content to file #node.js write to a file open file in node js #open html file in node.js #open pdf file in node js #open json file in node js #open file in browser node js #fs open file node js #open zip file node.js #open txt file node.js #open a file in node js #node js open html file in browser #open file in node #open file in nodejs #open file from node.js #how to open file in node.js #how to open json file in node js #open js file in node js #file open in node js #node js open file in new tab #how to open html file in node js #open file with 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 sync 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
Leave a Comment