Last active
August 23, 2023 16:31
-
-
Save jrc03c/55c46775a9e7c5ba4e115ac2cccb581f to your computer and use it in GitHub Desktop.
Stream (read) a file from disk in Node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require("fs") | |
const readline = require("readline") | |
!(async () => { | |
const stream = fs.createReadStream("path/to/file") | |
const rl = readline.createInterface({ | |
input: stream, | |
crlfDelay: Infinity, | |
}) | |
// optionally listen for errors | |
stream.on("error", error => { | |
throw error | |
}) | |
for await (const line of rl) { | |
// do something with `line` | |
} | |
stream.destroy() | |
rl.close() | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment