from2: Readable Stream Wrapper
from2 is a convenience wrapper for Node.js `ReadableStream` (specifically, the `readable-stream` base class), designed to simplify the creation of readable streams while correctly handling backpressure. The current stable version is 2.3.0, published in 2016, indicating the project is likely abandoned or in long-term maintenance with no active development or planned release cadence. It differentiates itself by offering an API inspired by `from` and `through2`, providing `from2.obj` for object mode streams and `from2.ctor` for creating reusable stream constructors, which can improve performance for multiple similar streams. It aims to make stream creation more approachable than direct `ReadableStream` implementation.
Common errors
-
TypeError: from2.obj is not a function
cause Attempting to import `obj` or `ctor` as a named ES module import, e.g., `import { obj } from 'from2'` in an ESM file.fixUse CommonJS `require` for `from2` and access properties directly: `const from = require('from2'); const myStream = from.obj(...)`. -
Error [ERR_REQUIRE_ESM]: require() of ES Module ... from2.js not supported.
cause Trying to `require('from2')` from an ES module file (`.mjs` or `type: module` in `package.json`) if a hypothetical future version of from2 were ESM-only, or if a build process incorrectly targets ESM for `from2`. This specific error is unlikely for `from2` itself given its age and CJS nature, but represents a common interop issue.fixEnsure your project or build system correctly handles `from2` as a CommonJS module. If you are in an ESM context and cannot avoid `from2`, use dynamic `import('from2')` and then access the default export's properties, e.g., `(await import('from2')).default.obj(...)`. Note that `from2` itself is CJS, so `require` should generally work.
Warnings
- gotcha The package displays an 'experimental' stability badge in its README. While functional, this historically implied that its API or behavior might not be fully stable or guaranteed for long-term production use without potential changes. Given its age, no further stabilization is expected.
- breaking This package is no longer actively maintained. The last release was over 7 years ago (as of 2026). This means it will not receive updates for new Node.js features, critical bug fixes, or security patches, potentially exposing applications to vulnerabilities or compatibility issues with newer environments.
- gotcha `from2` is built on CommonJS module syntax (`require`). Attempting to use ES module `import` syntax directly in an ESM context will likely fail or require complex interoperability solutions (e.g., dynamic `import()`), as named exports from CJS modules are not directly supported by `import { Name } from 'cjs-module'`.
Install
-
npm install from2 -
yarn add from2 -
pnpm add from2
Imports
- from
import from from 'from2'
const from = require('from2') - from.obj
import { obj } from 'from2'const from = require('from2'); const objStream = from.obj(...) - from.ctor
import { ctor } from 'from2'const from = require('from2'); const StreamConstructor = from.ctor(...)
Quickstart
const from = require('from2');
function fromString(string) {
return from(function(size, next) {
// if there's no more content
// left in the string, close the stream.
if (string.length <= 0) return next(null, null);
// Pull in a new chunk of text,
// removing it from the string.
var chunk = string.slice(0, size);
string = string.slice(size);
// Emit "chunk" from the stream.
next(null, chunk);
});
}
// pipe "hello world" out
// to stdout.
fromString('hello world').pipe(process.stdout);