![]() |
VOOZH | about |
In this article, we will return an array of lines from a specified file using node.js. The fs module is used to deal with the file system in node.js and to read file data we use fs.readFileSync( ) or fs.readFile( ) methods. Here we will use the readFileSync method to read the file, and we use the steps below to return the lines of the file in an array:
Table of Content
To return an array of lines from a file in Node.js, we will
Example:
Below is the Example in which we are implementing the above steps:
Text file: The gfg.txt file.
Geeksforgeeks
A computer Science Portal for GeeksSteps to Run the Code: use the following the command
node index.jsOutput:
[
'Geeksforgeeks',
'A computer Science Portal for Geeks'
]readline for Large FilesThis approach uses the readline module to read a file line-by-line, storing each line in an array, which is then returned via a callback function once the file reading is complete.
Example:
Output:
[
'Geeksforgeeks',
'A computer Science Portal for Geeks'
]