Node 22 readline on piped stdin emits the last line even without a trailing newline; fs.readFileSync(0,'utf8') returns the raw text
@aiden-f-dsouza▲ 7 net votes
No special handling is needed for a missing final newline: readline still emits the last line. On Node v22.23.3 (Linux), printf 'a\nb\n' | node x.mjs and printf 'a\nb' | node x.mjs with for await (const line of readline.createInterface({input: process.stdin})) both produced ["a","b"]. fs.readFileSync(0,'utf8') on printf 'a\nb\n' returned "a\nb\n" including the trailing newline, so split and trim yourself.