To get the contents of a file as a string, we can use the readFileSync()
or readFile()
functions from the native filesystem (fs
) module in Node.js.
/* Get all the contents from a file */
const content = readFileSync("myFile.txt");
- The
readFileSync()
function is used to synchronously read the contents from a file which further blocks the execution of code in Nodejs. - The
readFile()
function is used to asynchronously read the contents from a file in Node.js.
For example, Let's say we have a file called myFile.txt
and we want to read all the contents from it as a string.
First thing we have to do is require()
or import
the filesystem
module like this,
// get filesystem module
const fs = require("fs");
Using readFileSync() - Synchronous function
Now we can use the readFileSync()
function from the fs
module and pass the path to the file.
It can be done like this,
// get filesystem module
const fs = require("fs");
// using the readFileSync() function
// and passing the path to the file
const buffer = fs.readFileSync("myFile.txt");
console.log(buffer);
/*
<Buffer 4c 6f 72 65 6d 20 69 70 73 75 6d 20 64
6f 6c 6f 72 20 73 69 74 ... 7106 more bytes>
*/
- The
readFileSync()
function returns aBuffer
.
Since it is a Buffer
we can use the toString()
method on the Buffer
object to get the contents as String
.
It can be done like this,
// get filesystem module
const fs = require("fs");
// using the readFileSync() function
// and passing the path to the file
const buffer = fs.readFileSync("myFile.txt");
// use the toString() method to convert
// Buffer into String
const fileContent = buffer.toString();
console.log(fileContent);
/*
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Aliquet porttitor lacus luctus accumsan tortor posuere ac ut consequat....
...
*/
- Since the
myFile.txt
file is in the same folder as the running script, we don't need to resolve the path. But sometimes you need to resolve the path because the directory and the script may not be in the same directory, for that you can use thepath
module in Node.js to achieve that.
See this example live in repl.it.
Using readFile() - Asynchronous function
Now let's use the readFile()
function from the fs
module.
Here the function needs 2 arguments,
- The First argument is the path to the file.
- The second one is an error first callback function which will get executed when all the contents inside the file are read. The callback function receives an
error
object as the first parameter if any, and aBuffer
object as the second parameter.
It can be done like this,
// get filesystem module
const fs = require("fs");
// using the readFile() function
// and passing the path to the file
// as the first argument
// callback function as the second argument
// which will execute after contents of file read
fs.readFile("myFile.txt", (err, buff) => {
// if any error
if (err) {
console.error(err);
return;
}
// otherwise log contents
console.log(buff.toString());
/*
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Aliquet porttitor lacus luctus accumsan tortor posuere ac ut consequat....
...
*/
});
- Since it is a buffer, we can use the
toString()
method on theBuffer
object to get the contents asString
.
See this example live in repl.it.
And this is how we can read the contents from the file as String
.