{"id":14662,"library":"lazystream","title":"Lazy Node.js Streams","description":"lazystream is a Node.js utility library designed to address the \"too many open files\" (EMFILE) error by creating readable and writable streams on demand. Instead of eagerly opening file handles or other resources upon instantiation, it wraps stream creation functions, deferring the actual stream initialization until the stream is actively accessed (e.g., read from, written to, or piped). The current stable version is 1.0.1, released in 2016. The package provides `lazystream.Readable` and `lazystream.Writable` classes, which internally extend Node's `stream.PassThrough` by relying on `readable-stream@2.x` for a stable Streams3 API implementation. This approach allows developers to pass around stream proxies without immediate resource allocation, offering a simple, callback-based mechanism to encapsulate stream creation logic. However, the package is no longer actively maintained.","status":"abandoned","version":"1.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/jpommerening/node-lazystream","tags":["javascript","emfile","lazy","streams","stream"],"install":[{"cmd":"npm install lazystream","lang":"bash","label":"npm"},{"cmd":"yarn add lazystream","lang":"bash","label":"yarn"},{"cmd":"pnpm add lazystream","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Provides a stable Streams3 API implementation, decoupled from Node.js core versions, enabling consistent lazy stream functionality.","package":"readable-stream","optional":false}],"imports":[{"note":"The package is CommonJS-only. Destructuring assignment is common for class imports.","wrong":"import { Readable } from 'lazystream';","symbol":"Readable","correct":"const { Readable } = require('lazystream');"},{"note":"The package is CommonJS-only. Destructuring assignment is common for class imports.","wrong":"import { Writable } from 'lazystream';","symbol":"Writable","correct":"const { Writable } = require('lazystream');"},{"note":"The primary way to import the package as a whole, providing access to `lazystream.Readable` and `lazystream.Writable`.","wrong":"import lazystream from 'lazystream';","symbol":"lazystream (namespace)","correct":"const lazystream = require('lazystream');"}],"quickstart":{"code":"const lazystream = require('lazystream');\nconst fs = require('fs');\nconst path = require('path');\n\n// Create a dummy file for reading\nconst inputFile = path.join(__dirname, 'input.txt');\nfs.writeFileSync(inputFile, 'Hello, lazy stream!');\n\n// Define the output file\nconst outputFile = path.join(__dirname, 'output.txt');\n\nconsole.log('Creating lazy streams...');\n// Create a lazy readable stream\nconst lazyReadable = new lazystream.Readable(function () {\n  console.log('  -> Actually opening read stream now.');\n  return fs.createReadStream(inputFile);\n});\n\n// Create a lazy writable stream\nconst lazyWritable = new lazystream.Writable(function () {\n  console.log('  -> Actually opening write stream now.');\n  return fs.createWriteStream(outputFile);\n});\n\nconsole.log('Lazy streams instantiated, but file handles are not yet open.');\n\n// Pipe the lazy readable to the lazy writable\nlazyReadable.pipe(lazyWritable)\n  .on('finish', () => {\n    console.log('Piping finished. Content written to output.txt');\n    const content = fs.readFileSync(outputFile, 'utf8');\n    console.log('Output file content:', content);\n    // Clean up\n    fs.unlinkSync(inputFile);\n    fs.unlinkSync(outputFile);\n  })\n  .on('error', (err) => {\n    console.error('Stream error:', err);\n    // Clean up in case of error\n    if (fs.existsSync(inputFile)) fs.unlinkSync(inputFile);\n    if (fs.existsSync(outputFile)) fs.unlinkSync(outputFile);\n  });\n\nconsole.log('Piping operation initiated. Streams will open on first data access.');","lang":"javascript","description":"Demonstrates how to create lazy readable and writable streams using `lazystream`, deferring the actual file opening until data is piped, and then cleans up the temporary files."},"warnings":[{"fix":"Ensure compatibility with `readable-stream@2.x` and verify stream behavior if upgrading from pre-1.0.0 versions.","message":"Version 1.0.0 unconditionally switched its internal stream implementation to `readable-stream@2.x`. While this improved stability across Node.js versions, it represents a breaking change in internal dependency and potential behavior for users relying on specific `readable-stream` versions.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Evaluate active stream management libraries or implement custom lazy stream logic for long-term projects.","message":"The `lazystream` package is no longer actively maintained. Its last significant update was in 2016. Use at your own risk, and consider alternative solutions for modern Node.js applications.","severity":"deprecated","affected_versions":">=1.0.1"},{"fix":"Thoroughly test `lazystream` in your target Node.js environment, especially with recent Node.js releases. Be prepared to refactor if compatibility issues arise.","message":"Despite using `readable-stream` for Streams3 API compatibility, the package's age means it may not fully support or interact correctly with newer Node.js stream APIs (e.g., Streams4/5) introduced in later Node.js versions, potentially leading to unexpected behavior or incompatibilities.","severity":"gotcha","affected_versions":">=1.0.1"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Implement `lazystream.Readable` or `lazystream.Writable` to defer the creation of stream resources until they are actually needed, preventing premature resource exhaustion.","cause":"Attempting to open too many file descriptors simultaneously without proper resource management, exceeding system limits.","error":"Error: EMFILE, too many open files"},{"fix":"Ensure the object is a valid `lazystream.Readable` or `lazystream.Writable` instance (which extends `stream.PassThrough`), or a compatible Node.js Stream. Check Node.js and `readable-stream` versions for compatibility.","cause":"Attempting to call `.pipe()` on an object that is not a valid stream, or using an incompatible stream version/implementation.","error":"TypeError: stream.pipe is not a function"},{"fix":"Run `npm install lazystream --save` in your project directory to install the package.","cause":"The `lazystream` package was not installed or is not accessible in the current project's `node_modules`.","error":"Cannot find module 'lazystream'"}],"ecosystem":"npm"}