Node-Stream Utilities
Node-Stream is a utility library designed to simplify working with Node.js stream APIs by exposing a collection of array-like methods for consuming, creating, and manipulating streams. Originally aiming to resolve common misunderstandings and misuse of Node's core stream interface, it provides methods like `where`, `sort`, `take`, `stringify`, and `intersperse` that return Streams3 instances. The library was built to work with Node.js versions 0.12 and newer, supporting the Streams3 implementation. However, the package, currently at version 1.7.0, was last published approximately 9 years ago (as of April 2026), indicating it is no longer actively maintained and has been effectively abandoned. While it aimed to offer a more accessible API for Node.js streams, its age means it does not support modern Node.js stream features like `stream.promises` or async iterators, nor does it provide ESM compatibility.
Common errors
-
SyntaxError: Cannot use import statement outside a module
cause Attempting to use `import` syntax in a CommonJS module context or a Node.js project not configured for ESM.fixChange your import statement to `const nodeStream = require('node-stream');` or ensure your project's `package.json` specifies `"type": "module"` and handle CJS interop carefully. -
TypeError: (0 , node_stream_1.where) is not a function
cause This error often occurs when a CommonJS module, which exports an object, is incorrectly imported using an ESM named import syntax (e.g., `import { where } from 'node-stream';`). The bundler or runtime tries to destructure a non-existent export.fixUse CommonJS `require` to import the module and access its properties: `const nodeStream = require('node-stream');` then `nodeStream.where(...)` or `const { where } = require('node-stream');`. -
TypeError: db.createReadStream is not a function
cause The quickstart example assumes the existence of a `db` object with a `createReadStream` method. If you run the code without a proper mock or an actual database connection, this error will occur.fixEnsure that `db` is defined and has a `createReadStream` method that returns a readable stream, or replace it with a standard `fs.createReadStream` or a mock as shown in the quickstart example.
Warnings
- breaking This package is CommonJS-only and does not natively support ECMAScript Modules (ESM) `import` syntax. Attempting to use `import` will cause `ERR_REQUIRE_ESM` or similar errors in modern Node.js environments configured for ESM.
- gotcha The `node-stream` package is effectively abandoned, with its last update occurring approximately 9 years ago. This means it receives no further bug fixes, security patches, or feature enhancements. Using it in production applications poses significant risks, including potential security vulnerabilities and incompatibility with future Node.js versions or modern stream patterns (e.g., `async/await` with streams).
- gotcha Designed for Node.js 0.12+ and Streams3, this library predates many significant advancements in Node.js stream handling, such as `stream.pipeline` for robust error handling, `stream.finished`, and built-in support for `async iterators`. Its API may feel less ergonomic compared to modern approaches, and it lacks direct compatibility with newer stream features, potentially leading to increased boilerplate or inefficient patterns.
Install
-
npm install node-stream -
yarn add node-stream -
pnpm add node-stream
Imports
- nodeStream
import nodeStream from 'node-stream';
const nodeStream = require('node-stream'); - where
import { where } from 'node-stream';const { where } = require('node-stream'); - take
import { take } from 'node-stream';const { take } = require('node-stream');
Quickstart
const nodeStream = require('node-stream');
const { Readable } = require('stream');
// Mock a database read stream for demonstration purposes
const mockData = [
{ id: 1, type: 'post', author: 'stezu', content: 'First post' },
{ id: 2, type: 'comment', author: 'guest', content: 'Nice one!' },
{ id: 3, type: 'post', author: 'stezu', content: 'Second post' },
{ id: 4, type: 'post', author: 'other', content: 'Another post' },
{ id: 5, type: 'post', author: 'stezu', content: 'Third post' },
{ id: 6, type: 'post', author: 'stezu', content: 'Fourth post' },
{ id: 7, type: 'post', author: 'stezu', content: 'Fifth post' }
];
const db = {
createReadStream: () => {
let index = 0;
return new Readable({
objectMode: true,
read() {
if (index < mockData.length) {
this.push(mockData[index++]);
} else {
this.push(null);
}
}
});
}
};
// Get the 5 most recent posts by stezu
db.createReadStream()
.pipe(nodeStream.where({ type: 'post', author: 'stezu' }))
.pipe(nodeStream.sort((a, b) => a.id < b.id ? 1 : -1)) // Sort descending by ID for 'most recent'
.pipe(nodeStream.take(5))
.pipe(nodeStream.stringify()) // Convert objects to JSON strings
.pipe(nodeStream.intersperse('\n')) // Add newlines between items
.pipe(process.stdout);