{"id":27866,"library":"http-ndjson","title":"http-ndjson","description":"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.","status":"active","version":"3.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/yoshuawuyts/http-ndjson","tags":["javascript","http","log","logger","ndjson","standard","request","response","server"],"install":[{"cmd":"npm install http-ndjson","lang":"bash","label":"npm"},{"cmd":"yarn add http-ndjson","lang":"bash","label":"yarn"},{"cmd":"pnpm add http-ndjson","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is CJS-only; ES module imports will fail unless using a bundler that can handle them.","wrong":"import httpNdjson from 'http-ndjson'","symbol":"default","correct":"const httpNdjson = require('http-ndjson')"},{"note":"The package exports a single function; there is no named export.","wrong":"const log = require('http-ndjson').httpNdjson","symbol":"default","correct":"const httpNdjson = require('http-ndjson')"},{"note":"Destructuring will result in undefined; the function is the default export.","wrong":"const { httpNdjson } = require('http-ndjson')","symbol":"default","correct":"const httpNdjson = require('http-ndjson')"}],"quickstart":{"code":"const httpNdjson = require('http-ndjson')\nconst http = require('http')\n\nhttp.createServer((req, res) => {\n  const log = console.log\n  const setSize = httpNdjson(req, res, log)\n  const body = 'OK'\n  setSize(body.length)\n  res.end(body)\n}).listen(3000, () => console.log('Server listening on 3000'))","lang":"javascript","description":"Shows how to create an HTTP server and log requests/responses as ndjson using the default callback (console.log)."},"warnings":[{"fix":"Update your callback to accept the ndjson object directly (e.g., `console.log(msg)` instead of `console.log(msg1, msg2)`).","message":"Version 3.0.0 changed the API: the callback signature now receives the message object; previously it received separate arguments.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Always pass the correct byte length of the response body to the returned function (e.g., `setSize(Buffer.byteLength(body))`).","message":"The 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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use `require()` or configure your project to use CommonJS. Alternatively, use a dynamic import with a bundler that can handle CJS.","message":"The 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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"No action needed.","message":"The `opts` parameter can be a function for custom fields, but this is not deprecated yet. No specific deprecations noted.","severity":"deprecated","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-05-09T00:00:00.000Z","next_check":"2026-08-07T00:00:00.000Z","problems":[{"fix":"Use `const httpNdjson = require('http-ndjson')` without destructuring.","cause":"Using destructured import like `const { httpNdjson } = require('http-ndjson')`, which yields undefined.","error":"TypeError: httpNdjson is not a function"},{"fix":"Use dynamic import(): `import('http-ndjson').then(m => m.default)`, or change the project to CommonJS.","cause":"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\".","error":"ERR_REQUIRE_ESM: require() of ES Module not supported"},{"fix":"Ensure you pass a callback function as the third argument: `const setSize = httpNdjson(req, res, console.log)`","cause":"Calling httpNdjson without providing a callback, or passing a non-function as the last argument.","error":"TypeError: setSize is not a function"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}