{"name":"n-readlines","version":"3.3.0","keywords":["read","line","reader","linereader","readfile","linebyline","synchronous","sync","readline","readlines"],"author":{"name":"Yoan Arnaudov","email":"jonidev@gmail.com"},"license":"MIT","_id":"n-readlines@3.3.0","maintainers":[{"name":"nacholibre","email":"jonidev@gmail.com"}],"homepage":"https://github.com/nacholibre/node-readlines#readme","bugs":{"url":"https://github.com/nacholibre/node-readlines/issues"},"dist":{"shasum":"936f991ae09af70aae760324d51bbe520cbaf971","tarball":"http://registry-npm.ywwl.com/n-readlines/download/n-readlines-3.3.0.tgz","fileCount":23,"integrity":"sha512-h+QuqcC7KyzoZF4crC7ISFpvAvPi4BplrDm9CRMPMONd8P9UojVYDbQYXKqusOA6cL92o7QvTiMcEsfpXWkL6g==","signatures":[{"sig":"MEYCIQChBELm3k4ZKBr7umFuNdCB0kIsiFjkyblT0zuxbduUqwIhAJYOhXWgJ8qoZyFjrio/GJRz0t+chsaQhKeZqIzCP7j/","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":113383,"size":32903,"noattachment":false,"key":"/n-readlines/-/n-readlines-3.3.0.tgz"},"main":"./readlines.js","types":"./readlines.d.ts","engines":{"node":">=18.x.x"},"gitHead":"2ad266989976eb8612832b10353594fa7f332246","scripts":{"test":"node --test test/*.test.js"},"_npmUser":{"name":"nacholibre","email":"jonidev@gmail.com"},"repository":{"url":"git+ssh://git@github.com/nacholibre/node-readlines.git","type":"git"},"_npmVersion":"10.9.2","description":"Read file line by line without buffering the whole file in memory.","directories":{},"_nodeVersion":"23.11.0","dependencies":{},"_hasShrinkwrap":false,"devDependencies":{},"_npmOperationalInternal":{"tmp":"tmp/n-readlines_3.3.0_1765184530741_0.8829263043105458","host":"s3://npm-registry-packages-npm-production"},"readme":"# n-readlines\n\n[![Tests](https://github.com/nacholibre/node-readlines/actions/workflows/test.yml/badge.svg)](https://github.com/nacholibre/node-readlines/actions/workflows/test.yml)\n[![npm version](https://img.shields.io/npm/v/n-readlines.svg)](https://www.npmjs.com/package/n-readlines)\n[![npm downloads](https://img.shields.io/npm/dm/n-readlines.svg)](https://www.npmjs.com/package/n-readlines)\n[![license](https://img.shields.io/npm/l/n-readlines.svg)](https://github.com/nacholibre/node-readlines/blob/master/LICENSE)\n[![TypeScript](https://img.shields.io/badge/TypeScript-ready-blue.svg)](https://www.typescriptlang.org/)\n[![Bun](https://img.shields.io/badge/Bun-compatible-f9f1e1.svg)](https://bun.sh/)\n[![Deno](https://img.shields.io/badge/Deno-compatible-000000.svg)](https://deno.land/)\n\n> 📖 Read files line-by-line, synchronously. Zero dependencies.\n\nReading a file line by line may seem trivial, but in Node.js there's no straightforward way to do it. Many libraries use Transform Streams which feels like overkill for such a simple task. This library uses only Node's built-in `fs` module to provide a clean, synchronous API.\n\n## ✨ Features\n\n- 🚀 **Simple API** — just `next()` to get the next line\n- 📦 **Zero dependencies** — only uses Node.js built-ins\n- 🔄 **Synchronous** — no callbacks or promises to manage\n- 💾 **Memory efficient** — reads in chunks, doesn't load entire file\n- 🔧 **Configurable** — custom chunk sizes\n- 📘 **TypeScript support** — includes type definitions\n- 🪟 **Cross-platform** — handles LF, CRLF, and CR line endings automatically\n- 📥 **Stdin support** — read from stdin by passing fd 0\n- 🥟 **Bun compatible** — works with Bun runtime\n- 🦕 **Deno compatible** — works with Deno runtime\n\n## 📦 Installation\n\n```bash\nnpm install n-readlines\n```\n\n**Requirements:** Node.js >= 18.x, Bun, or Deno\n\n## 🚀 Quick Start\n\n```javascript\nconst LineByLine = require('n-readlines');\nconst liner = new LineByLine('./textfile.txt');\n\nlet line;\nwhile (line = liner.next()) {\n    console.log(line.toString());\n}\n```\n\n### TypeScript\n\n```typescript\nimport LineByLine = require('n-readlines');\nconst liner = new LineByLine('./textfile.txt');\n\nlet line: Buffer | null;\nwhile (line = liner.next()) {\n    console.log(line.toString());\n}\n```\n\n## 📖 API Reference\n\n### Constructor\n\n```javascript\nnew LineByLine(filename, [options])\nnew LineByLine(fd, [options])\n```\n\n| Parameter | Type | Description |\n|-----------|------|-------------|\n| `filename` | `string` | Path to the file to read |\n| `fd` | `number` | File descriptor (0 for stdin, or from `fs.openSync`) |\n| `options.readChunk` | `number` | Bytes to read at once. Default: `1024` |\n\n### Methods\n\n#### `.next()` → `Buffer | null`\n\nReturns the next line as a `Buffer` (without the newline character), or `null` when end of file is reached.\n\n```javascript\nconst line = liner.next();\nif (line !== null) {\n    console.log(line.toString()); // Convert Buffer to string\n}\n```\n\n#### `.reset()`\n\nResets the reader to the beginning of the file.\n\n```javascript\nliner.next(); // Read first line\nliner.next(); // Read second line\nliner.reset(); // Go back to start\nliner.next(); // First line again\n```\n\n> **Note:** `reset()` does not work with stdin.\n\n#### `.close()`\n\nManually closes the file. Subsequent `next()` calls will return `null`.\n\n```javascript\nliner.next();\nliner.close(); // Done reading early\nliner.next(); // Returns null\n```\n\n> **Note:** When reading from stdin, `close()` does not close the stdin stream.\n\n#### `.isLast()` → `boolean`\n\nReturns `true` if the last line has been read and there are no more lines available.\n\n```javascript\nconst liner = new LineByLine('./file.txt');\n\nwhile (true) {\n    const line = liner.next();\n    if (line === null) break;\n    \n    console.log(line.toString());\n    \n    if (liner.isLast()) {\n        console.log('This was the last line!');\n    }\n}\n```\n\n## 📚 Examples\n\n### Basic line reading\n\n```javascript\nconst LineByLine = require('n-readlines');\nconst liner = new LineByLine('./data.txt');\n\nlet line;\nlet lineNumber = 1;\n\nwhile (line = liner.next()) {\n    console.log(`Line ${lineNumber}: ${line.toString('utf8')}`);\n    lineNumber++;\n}\n\nconsole.log('Finished reading file');\n```\n\n### Reading from stdin\n\n```javascript\nconst LineByLine = require('n-readlines');\nconst liner = new LineByLine(0); // fd 0 = stdin\n\nlet line;\nwhile (line = liner.next()) {\n    console.log(line.toString());\n}\n```\n\nUsage:\n```bash\necho -e \"line1\\nline2\\nline3\" | node script.js\ncat file.txt | node script.js\n```\n\n### Reading with custom chunk size\n\n```javascript\nconst liner = new LineByLine('./large-file.txt', {\n    readChunk: 4096 // Read 4KB at a time\n});\n```\n\n### Processing JSON lines (JSONL/NDJSON)\n\n```javascript\nconst LineByLine = require('n-readlines');\nconst liner = new LineByLine('./data.jsonl');\n\nlet line;\nwhile (line = liner.next()) {\n    const record = JSON.parse(line.toString());\n    console.log(record);\n}\n```\n\n### Early termination\n\n```javascript\nconst liner = new LineByLine('./log.txt');\n\nlet line;\nwhile (line = liner.next()) {\n    const text = line.toString();\n    \n    if (text.includes('ERROR')) {\n        console.log('Found error:', text);\n        liner.close(); // Stop reading\n        break;\n    }\n}\n```\n\n## 📝 Notes\n\n- Lines are returned as `Buffer` objects — call `.toString()` to convert to string\n- The newline character is **not** included in the returned line\n- Files without a trailing newline are handled correctly\n- Empty lines are preserved and returned as empty buffers\n- Returns `null` (not `false`) when end of file is reached\n\n### Line Ending Support\n\nThe library automatically handles all common line ending formats:\n\n| Format | Characters | Platform |\n|--------|------------|----------|\n| **LF** | `\\n` | Unix, Linux, macOS |\n| **CRLF** | `\\r\\n` | Windows |\n| **CR** | `\\r` | Classic Mac OS |\n\nFiles with mixed line endings are also supported — each line is detected individually.\n\n## 📄 License\n\nMIT © [Yoan Arnaudov](https://github.com/nacholibre)\n","publish_time":1765184530916,"_cnpm_publish_time":1765184530916,"dist-tags":{"latest":"3.4.1"}}