{"id":13430,"library":"ldjson-stream","title":"LDJSON Stream Parser & Serializer","description":"ldjson-stream is a streaming library designed for parsing and serializing line-delimited JSON (also known as NDJSON or JSON Lines). It provides two primary Transform streams: `parse()` for converting newline-separated JSON strings into JavaScript objects, and `serialize()` for converting JavaScript objects into newline-delimited JSON strings. The package is lightweight and was last updated over a decade ago, with its current stable version being 1.2.1. Due to its age, it exclusively uses CommonJS modules and does not offer native ESM support or TypeScript type definitions. While it remains functional for basic NDJSON processing in older Node.js environments, its abandoned status means it lacks ongoing maintenance, security updates, or compatibility enhancements for modern JavaScript ecosystems. It was a minimalist solution for streaming JSON in its time.","status":"abandoned","version":"1.2.1","language":"javascript","source_language":"en","source_url":"git://github.com/maxogden/ldjson-stream","tags":["javascript"],"install":[{"cmd":"npm install ldjson-stream","lang":"bash","label":"npm"},{"cmd":"yarn add ldjson-stream","lang":"bash","label":"yarn"},{"cmd":"pnpm add ldjson-stream","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is CommonJS-only and does not support ES module imports. Attempting to use `import` will result in a module resolution error.","wrong":"import ldj from 'ldjson-stream'","symbol":"ldj","correct":"const ldj = require('ldjson-stream')"},{"note":"`parse` is a method of the default exported CommonJS module, not a named export. It returns a Transform stream.","wrong":"import { parse } from 'ldjson-stream'","symbol":"ldj.parse","correct":"const ldj = require('ldjson-stream');\nconst parseStream = ldj.parse();"},{"note":"`serialize` is a method of the default exported CommonJS module, not a named export. It returns a Transform stream.","wrong":"import { serialize } from 'ldjson-stream'","symbol":"ldj.serialize","correct":"const ldj = require('ldjson-stream');\nconst serializeStream = ldj.serialize();"}],"quickstart":{"code":"const fs = require('fs');\nconst { Readable } = require('stream');\nconst ldj = require('ldjson-stream');\n\nconst jsonData = [\n  { id: 1, name: 'Alice', role: 'Engineer' },\n  { id: 2, name: 'Bob', role: 'Designer' },\n  { id: 3, name: 'Charlie', role: 'PM' }\n];\n\n// Create a readable stream from an array of objects\nconst inputReadable = new Readable({\n  objectMode: true,\n  read() {\n    if (jsonData.length) {\n      this.push(jsonData.shift());\n    } else {\n      this.push(null);\n    }\n  }\n});\n\n// Simulate reading from a file, serializing, and then parsing\ninputReadable\n  .pipe(ldj.serialize())\n  .pipe(ldj.parse({ strict: false })) // Use strict: false to be resilient to malformed lines if needed\n  .on('data', (obj) => {\n    console.log('Parsed object:', obj);\n  })\n  .on('end', () => {\n    console.log('All objects processed.');\n  })\n  .on('error', (err) => {\n    console.error('Stream error:', err.message);\n  });\n","lang":"javascript","description":"Demonstrates how to use `ldj.serialize()` to convert JavaScript objects into line-delimited JSON and then `ldj.parse()` to convert them back into objects, simulating a full data stream."},"warnings":[{"fix":"Migrate to actively maintained NDJSON streaming libraries like `ndjson` or `JSONStream` or `ld-jsonstream` (a different package with a similar name) for production use.","message":"The package is abandoned and has not been updated in over a decade. It does not receive security patches, bug fixes, or compatibility updates for newer Node.js versions or JavaScript features. Consider modern alternatives for new projects.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"For ES Module projects, consider dynamic `import('ldjson-stream')` or use a bundler (e.g., Webpack, Rollup) that handles CommonJS modules. Preferably, migrate to an ESM-native NDJSON library.","message":"This library is strictly CommonJS. It cannot be directly imported into an ES Module project using `import` statements without Node.js's CJS-ESM interop features or a bundler configured for CJS fallback.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Create a `ldjson-stream.d.ts` declaration file or use a modern, actively maintained library that provides built-in TypeScript support.","message":"The library does not ship with TypeScript type definitions. Projects using TypeScript will require manual type declarations or `@ts-ignore` directives to use `ldjson-stream` safely.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"To gracefully handle invalid JSON lines, initialize the parser with `ldj.parse({ strict: false })`. This will cause the parser to discard malformed lines without emitting an error, allowing the stream to continue.","message":"By default, `ldj.parse()` operates in 'strict' mode, throwing a `SyntaxError` if any line contains invalid JSON. This can halt stream processing unexpectedly with malformed input.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Use the CommonJS `require` syntax: `const ldj = require('ldjson-stream');` and access methods as `ldj.parse()` and `ldj.serialize()`.","cause":"Attempting to import `ldjson-stream` using ES Module syntax (e.g., `import { parse } from 'ldjson-stream'`) or incorrectly destructing the CommonJS export.","error":"TypeError: ldj.parse is not a function"},{"fix":"If malformed JSON lines should be ignored, instantiate the parser with `{ strict: false }`: `ldj.parse({ strict: false })`. Otherwise, ensure input data integrity.","cause":"The input stream contains a line that is not valid JSON, and the `ldj.parse()` stream is operating in its default strict mode.","error":"SyntaxError: Unexpected token <char> in JSON at position <number>"},{"fix":"Either configure your bundler to handle CommonJS modules, use dynamic import `import('ldjson-stream').then(ldj => ...)`, or for Node.js, ensure `require()` is used in a CJS context. The recommended long-term fix is to migrate to an ESM-compatible library.","cause":"An ES Module-enabled environment (e.g., a modern Node.js project with `\"type\": \"module\"` or a frontend bundler) is attempting to resolve `ldjson-stream` as an ESM module, but it's CJS-only.","error":"Module not found: Can't resolve 'ldjson-stream' in <path>"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null}