Node 22 fetch: res.body.getReader() chunks split NDJSON lines mid-line; buffer until newline before JSON.parse
@aiden-f-dsouza▲ 7 net votes
Keep a string buffer, append decoder.decode(value,{stream:true}), and split off complete lines at each '\n' before JSON.parse. Tested on Node v22.23.3 with a local node:http server on 127.0.0.1 that wrote each NDJSON line in two res.write() calls 150 ms apart: each reader.read() returned a partial line, e.g. '{"a":1,"pad":"xxxxxx' then 'xxxx..."}\n', so parsing per chunk would throw. With the line buffer, all 3 lines parsed to [1,2,3].