dev-null Stream Sink

0.1.1 · active · verified Sun Apr 19

The `dev-null` package for Node.js provides a straightforward implementation of a `/dev/null` stream, consuming all piped input data without emitting any output. This utility is particularly useful in testing, debugging, or scenarios where stream data needs to be discarded to prevent side effects, such as when monitoring stream events rather than their payload. Currently at version 0.1.1, the package is exceptionally stable due to its singular, well-defined purpose, and is not expected to receive frequent updates or new features. Its key differentiator is its minimal API and focused functionality, making it a drop-in solution for stream nullification without introducing unnecessary overhead or complex configurations, serving as a direct parallel to the Unix `/dev/null` device for data streams.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates piping a readable stream into dev-null to silence its output, contrasting it with a stream that outputs data.

const devnull = require('dev-null');
const { Readable } = require('stream');

// A simple readable stream that emits numbers
class NumberReadable extends Readable {
  constructor(options) {
    super(options);
    this.current = 0;
    this.to = options.to || 5;
  }

  _read() {
    if (this.current > this.to) {
      this.push(null); // No more data
      return;
    }
    this.push(Buffer.from(String(this.current++)));
  }
}

console.log('--- Piping numbers to dev-null (no output expected) ---');

new NumberReadable({ to: 5 })
  .pipe(devnull())
  .on('data', (d) => {
    // This event should ideally not fire if dev-null works correctly
    console.log('Unexpected data received:', d.toString());
  })
  .on('end', () => {
    console.log('Stream ended after piping to dev-null. This message indicates successful consumption.');
  });

console.log('\n--- Example without dev-null (output expected) ---');

new NumberReadable({ to: 2 })
  .on('data', (d) => {
    console.log('Data without dev-null:', d.toString());
  })
  .on('end', () => {
    console.log('Stream ended without dev-null. This message follows expected data output.');
  });

view raw JSON →