Registry / http-networking / http-ndjson

http-ndjson

JSON →
library3.1.0jsnpmunverified

Log HTTP requests and responses as ndjson (newline-delimited JSON). Version 3.1.0 is the latest stable release. It is designed to work well with bole and garnish for pretty-printing. Minimal dependencies and a focus on standard request/response properties. Use it to produce structured logs that can be piped to other ndjson-consuming tools.

npm install http-ndjson
INSTALL
IMPORT
SIG · HTTP-NDJSON
H
http-ndjson
http-networkingjavascriptv3.1.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.

default
const httpNdjson = require('http-ndjson')
import httpNdjson from 'http-ndjson'
This package is CJS-only; ES module imports will fail unless using a bundler that can handle them.
default
const httpNdjson = require('http-ndjson')
const log = require('http-ndjson').httpNdjson
The package exports a single function; there is no named export.
default
const httpNdjson = require('http-ndjson')
const { httpNdjson } = require('http-ndjson')
Destructuring will result in undefined; the function is the default export.

Shows how to create an HTTP server and log requests/responses as ndjson using the default callback (console.log).

const httpNdjson = require('http-ndjson') const http = require('http') http.createServer((req, res) => { const log = console.log const setSize = httpNdjson(req, res, log) const body = 'OK' setSize(body.length) res.end(body) }).listen(3000, () => console.log('Server listening on 3000'))
Debug
Known issues
breakingVersion 3.0.0 changed the API: the callback signature now receives the message object; previously it received separate arguments.
fix
Update your callback to accept the ndjson object directly (e.g., `console.log(msg)` instead of `console.log(msg1, msg2)`).
affects: >=3.0.0
gotchaThe function returns a `setSize` function that must be called with the response body length. Calling it with the wrong value may cause incorrect logging of response size.
fix
Always pass the correct byte length of the response body to the returned function (e.g., `setSize(Buffer.byteLength(body))`).
affects: >=1.0.0
gotchaThe package does not support ES modules (ESM). Attempting to import it with `import` will throw an error in Node.js if the project is configured as ESM.
fix
Use `require()` or configure your project to use CommonJS. Alternatively, use a dynamic import with a bundler that can handle CJS.
affects: >=1.0.0
deprecatedThe `opts` parameter can be a function for custom fields, but this is not deprecated yet. No specific deprecations noted.
fix
No action needed.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: httpNdjson is not a function
Using destructured import like `const { httpNdjson } = require('http-ndjson')`, which yields undefined.
fix
Use `const httpNdjson = require('http-ndjson')` without destructuring.
ERR_REQUIRE_ESM: require() of ES Module not supported
Attempting to require() the package in an ESM-only project, but the package is CJS. This error occurs when the project's package.json has "type": "module".
fix
Use dynamic import(): `import('http-ndjson').then(m => m.default)`, or change the project to CommonJS.
TypeError: setSize is not a function
Calling httpNdjson without providing a callback, or passing a non-function as the last argument.
fix
Ensure you pass a callback function as the third argument: `const setSize = httpNdjson(req, res, console.log)`
Upgrade
Version history
3.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
12 hits · last 30 days
node
10
OpenAI (training)
2
Resources
http-ndjson — npm install http-ndjson · libregistry