Skip to content

Instantly share code, notes, and snippets.

@jrc03c
Last active August 23, 2023 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrc03c/55c46775a9e7c5ba4e115ac2cccb581f to your computer and use it in GitHub Desktop.
Save jrc03c/55c46775a9e7c5ba4e115ac2cccb581f to your computer and use it in GitHub Desktop.
Stream (read) a file from disk in Node
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