How to return an array of lines from a file in node.js ? Last Updated : 18 Jun, 2024 Comments Improve Suggest changes Like Article Like Report 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 Using fs.readFileSync and split methodUsing readline for Large FilesUsing fs.readFileSync and split methodTo return an array of lines from a file in Node.js, we will Read the data of the file using the fs.readFileSync method, you will get a BufferConvert the Buffer into a string Using the toString( ) methodNow use the String.split() method to break the data and the delimiter should be "\n"Example: Below is the Example in which we are implementing the above steps: JavaScript // Requiring the fs module const fs = require("fs") // Creating a function which takes a file as input const readFileLines = filename => fs .readFileSync(filename) .toString('UTF8') .split('\n'); // Driver code let arr = readFileLines('gfg.txt'); // Print the array console.log(arr); 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' ]Using 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: Node // Requiring the fs and readline modules const fs = require("fs"); const readline = require("readline"); // Creating a function which takes a file as input const readFileLinesReadline = (filename, callback) => { const lines = []; const readInterface = readline.createInterface({ input: fs.createReadStream(filename), output: process.stdout, console: false }); readInterface.on('line', (line) => { lines.push(line); }); readInterface.on('close', () => { callback(null, lines); }); readInterface.on('error', (err) => { callback(err); }); }; // Driver code readFileLinesReadline('gfg.txt', (err, arrReadline) => { if (err) { console.error(err); return; } // Print the array console.log(arrReadline); }); Output: [ 'Geeksforgeeks', 'A computer Science Portal for Geeks' ] Comment More infoAdvertise with us Next Article How to return an array of lines from a file in node.js ? devi_johns Follow Improve Article Tags : Web Technologies Node.js Node.js-fs-module NodeJS-Questions Similar Reads How To Remove Blank Lines From a .txt File In Node.js? Removing blank lines from a text file is a common task when working with data in text format. Blank lines can occur due to formatting errors, data entry mistakes, or during file generation. Node.js, with its non-blocking I/O and file system capabilities, makes it easy to automate this process.In thi 3 min read How to Return an Array from Async Function in Node.js ? Asynchronous programming in Node.js is fundamental for handling operations like network requests, file I/O, and database queries without blocking the main thread. One common requirement is to perform multiple asynchronous operations and return their results as an array. This article explores how to 4 min read How to Add Data in JSON File using Node.js ? JSON stands for Javascript Object Notation. It is one of the easiest ways to exchange information between applications and is generally used by websites/APIs to communicate. For getting started with Node.js, refer this article.Prerequisites:NPM NodeApproachTo add data in JSON file using the node js 4 min read How to display output data in tabular form in Node.js ? Tables are a combination of rows and columns. Node.js has its own module named as a table that helps in making tables with a wide variety of styles that can be applied to the table such as border styles, colors body style, etc. Installation of module: npm install table Syntax: table(data, config) Pa 2 min read How To Read a File Line By Line Using Node.js? To read a file line by line in Node.js, there are several approaches that efficiently handle large files and minimize memory usage. In this article, we'll explore two popular approaches: using the Readline module (which is built into Node.js) and using the Line-reader module, a third-party package. 3 min read How to Show the Line which Cause the Error in Node.js ? Debugging is a critical part of software development. When an error occurs in a Node.js application, understanding exactly where it happened is essential for diagnosing and fixing the problem. Node.js provides several ways to pinpoint the line of code that caused an error. This article explores thes 4 min read How to read and write files in Node JS ? NodeJS provides built-in modules for reading and writing files, allowing developers to interact with the file system asynchronously. These modules, namely fs (File System), offer various methods for performing file operations such as reading from files, writing to files, and manipulating file metada 2 min read How to Import all Exports of a File as an Object in Node.js ? In Node.js, you can import all exports of a file as an object using the ES Modules syntax (import) or the CommonJS syntax (require). This approach is useful when you have multiple exports from a module and want to access them conveniently through a single object interface. Below are the approaches t 4 min read How to work with Node.js and JSON file ? Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. With node.js as backend, the developer can maintain a single codebase for an entire application in javaScript.JSON on the other hand, stands for JavaScript Object Notation. It is a lightweight format for storing and exchanging d 4 min read How to get raw content from a string including carriage return ? The raw content of a string includes the carriage returns, i.e. it contains the new line escape sequence. Note: The line feed, represented by "\n" and a carriage return "\r" are very different. A line feed means moving the cursor one line forward. A carriage return means moving the cursor to the beg 2 min read Like