Registry / storage / fetch-ndjson

fetch-ndjson

JSON →
library1.2.0jsnpmunverified

fetch-ndjson (v1.2.0) is a lightweight library that converts newline-delimited JSON (NDJSON) streaming responses from the Fetch API into async generators yielding parsed JSON objects. It handles incomplete lines and backpressure, and is designed for use with the Web Streams API (ReadableStream). Unlike heavier streaming parsers, it focuses solely on NDJSON and works in both browser and Node (v18+ with global fetch). Ships TypeScript types. Low maintenance; no recent updates.

npm install fetch-ndjson
INSTALL
IMPORT
SIG · FETCH-NDJSON
F
fetch-ndjson
storagejavascriptv1.2.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

ndjson
✓ import ndjson from 'fetch-ndjson'
✗ import { ndjson } from 'fetch-ndjson'
Default export only; named import will fail.
ndjson
✓ const ndjson = require('fetch-ndjson').default
✗ const ndjson = require('fetch-ndjson')
CJS users must require the default property; breaking in v2+.
ndjson
✓ import ndjson from 'fetch-ndjson'
✗ import * as ndjson from 'fetch-ndjson'
Namespace import not supported.

Demonstrates consuming an NDJSON stream using fetch, getReader, and an async generator with for-await-of loop.

import ndjson from 'fetch-ndjson'; async function run() { const response = await fetch('https://example.com/stream'); const reader = response.body.getReader(); const gen = ndjson(reader); for await (const value of gen) { console.log('Parsed object:', value); } } run().catch(console.error);
Debug
Known issues
breakingIn v1.0.0, ndjson returned an iterator directly; in v2.0.0 it returns an async generator. Code using for-await-of remains compatible, but manual .next() calls may break if not updated.
fix
Update to use async generator syntax: for await (const val of ndjson(reader))
affects: >=2.0.0
gotchandjson expects a ReadableStreamDefaultReader, not the stream itself. Passing response.body instead of response.body.getReader() will throw a TypeError.
fix
Always call .getReader() on the readable stream before passing to ndjson.
affects: *
gotchaThe generator does not automatically close the reader when the stream ends or an error occurs; you must handle cancellation manually.
fix
Use try/finally or AbortController to cancel the reader.
affects: *
deprecatedNode.js <18 may not have global fetch or ReadableStream; polyfills required.
fix
Use node-fetch (v3) or undici with fetch polyfill, and ensure ReadableStream is available.
affects: *
Errors
Common errors & fixes
TypeError: ndjson is not a function
Using named import instead of default import, or wrong CJS import syntax.
fix
Use `import ndjson from 'fetch-ndjson'` or `const ndjson = require('fetch-ndjson').default`.
TypeError: Cannot read properties of undefined (reading 'getReader')
Passing response.body (undefined) because fetch response is not a stream.
fix
Ensure the fetch request returns a streaming response, or check response.ok.
Upgrade
Version history
1.2.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
40 hits · last 30 days
node
34
Amazon
1
Resources
fetch-ndjson — npm install fetch-ndjson · libregistry