Node.js : Asynchronously Read from Files

Use the filesystem module for all file operations:

const fs = require(‘fs’);

With Encoding

In this example, read hello.txt from the directory /tmp. This operation will be completed in the background and the callback occurs on comple…


This content originally appeared on DEV Community and was authored by Rajesh Kumar Yadav

Use the filesystem module for all file operations:

const fs = require('fs');

With Encoding

In this example, read hello.txt from the directory /tmp. This operation will be completed in the background and the callback occurs on completion or failure:

fs.readFile('/tmp/hello.txt', { encoding: 'utf8' }, (err, content) => {
 // If an error occurred, output it and return
 if(err) return console.error(err);
 // No error occurred, content is a string
 console.log(content);
});

Without Encoding

Read the binary file binary.txt from the current directory, asynchronously in the background. Note that we do not set the 'encoding' option - this prevents Node.js from decoding the contents into a string:

fs.readFile('binary', (err, binaryContent) => {
 // If an error occurred, output it and return
 if(err) return console.error(err);
 // No error occurred, content is a Buffer, output it in
 // hexadecimal representation.
 console.log(content.toString('hex'));
});

Relative paths

Keep in mind that, in general case, your script could be run with an arbitrary current working directory. To address
a file relative to the current script, use __dirname or __filename:

fs.readFile(path.resolve(__dirname, 'someFile'), (err, binaryContent) => {
 //Rest of code
}

Buy Me A Coffee

With all that being said, I highly recommend you keep learning!

Thank you for reading this article. Please feel free to connect with me on LinkedIn and Twitter.


This content originally appeared on DEV Community and was authored by Rajesh Kumar Yadav


Print Share Comment Cite Upload Translate Updates
APA

Rajesh Kumar Yadav | Sciencx (2021-05-14T03:54:10+00:00) Node.js : Asynchronously Read from Files. Retrieved from https://www.scien.cx/2021/05/14/node-js-asynchronously-read-from-files/

MLA
" » Node.js : Asynchronously Read from Files." Rajesh Kumar Yadav | Sciencx - Friday May 14, 2021, https://www.scien.cx/2021/05/14/node-js-asynchronously-read-from-files/
HARVARD
Rajesh Kumar Yadav | Sciencx Friday May 14, 2021 » Node.js : Asynchronously Read from Files., viewed ,<https://www.scien.cx/2021/05/14/node-js-asynchronously-read-from-files/>
VANCOUVER
Rajesh Kumar Yadav | Sciencx - » Node.js : Asynchronously Read from Files. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/14/node-js-asynchronously-read-from-files/
CHICAGO
" » Node.js : Asynchronously Read from Files." Rajesh Kumar Yadav | Sciencx - Accessed . https://www.scien.cx/2021/05/14/node-js-asynchronously-read-from-files/
IEEE
" » Node.js : Asynchronously Read from Files." Rajesh Kumar Yadav | Sciencx [Online]. Available: https://www.scien.cx/2021/05/14/node-js-asynchronously-read-from-files/. [Accessed: ]
rf:citation
» Node.js : Asynchronously Read from Files | Rajesh Kumar Yadav | Sciencx | https://www.scien.cx/2021/05/14/node-js-asynchronously-read-from-files/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.