npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@jadujoel/node-shell

v0.2.2

Published

A lightweight and flexible shell execution library for Node.js that enables running shell commands with a clean and extensible API. This library provides a powerful `$` template function for executing commands, as well as methods to control command execut

Readme

Node Shell

A lightweight and flexible shell execution library for Node.js that enables running shell commands with a clean and extensible API. This library provides a powerful $ template function for executing commands, as well as methods to control command execution, parse output in various formats, handle errors gracefully, and even stream input and output.

Features

  • Simple command execution using a tagged template function.
  • Streaming stdout line-by-line as the command runs, without waiting for completion.
  • Access to stdin for sending data to running processes.
  • Flexible error handling (fail loudly by default, or opt into non-throwing mode).
  • Quiet mode to suppress output from being printed to the terminal.
  • Easy environment variable injection.
  • Parsing output as text, JSON, ArrayBuffer, or Blob.
  • Timeout support to terminate long-running commands.

Installation

Install the package using npm:

npm install @jadujoel/node-shell

Or using Yarn:

yarn add @jadujoel/node-shell

Usage

Importing the Library

import { $ } from '@jadujoel/node-shell';

Running a Command

Run a simple shell command and capture its output:

const result = await $`echo Hello, World!`.text();
console.log(result); // Output: "Hello, World!\n"

Using Environment Variables

Set custom environment variables for the command:

const env = { GREETING: "Hello" };
const result = await $`echo $GREETING, World!`.env(env).text();
console.log(result); // Output: "Hello, World!\n"

Handling Errors

Fail Quietly

Use .nothrow() to prevent errors from being thrown, and instead handle them in the result:

const result = await $`nonexistent-command`.nothrow().quiet().text();
console.log(result); // Output: "" (empty string, command failed but no error thrown)

Fail Loudly

By default, errors are thrown when a command fails:

try {
  await $`nonexistent-command`.text();
} catch (error) {
  console.error(error.message);
  // Possible output: "/bin/sh: nonexistent-command: command not found"
}

Working with JSON

Parse the output of a shell command as JSON:

const json = await $`echo '{"key":"value"}'`.quiet().json();
console.log(json); // Output: { key: 'value' }

Working with Binary Data

ArrayBuffer

Capture the output of a command as an ArrayBuffer:

const buffer = await $`echo Hello`.quiet().arrayBuffer();
console.log(buffer.constructor.name); // Output: "ArrayBuffer"

Blob

Similarly, capture output as a Blob:

const blob = await $`echo Hello`.quiet().blob();
console.log(blob.size); // Output: 6

Accessing Stdin

You can write to the stdin of a running command:

const proc = $`cat`.quiet().nothrow();
proc.stdin.write("Hello from stdin!\n");
proc.stdin.end();

const result = await proc;
console.log(result.text()); // Output: "Hello from stdin!\n"

Printing Each Line as it is Received

The .lines() method returns an async iterable that yields lines as they are emitted by the command's stdout. This allows you to process output in real-time, rather than waiting for the command to finish.

const start = Date.now();
{
  const shell = $`echo "Hello, World!" && sleep 1 && echo "Goodbye, World!" && sleep 1`;

  let i = 0;
  for await (const line of shell.lines()) {
    const timeTaken = Date.now() - start;
    console.log(`Line ${i++}: ${line} [${timeTaken}ms]`);
  }

  console.log(`Total time taken: ${Date.now() - start}ms`);
}
// This will print the first line after ~0ms, then the second line after ~1000ms.

Controlling Execution

  • quiet(): Suppress stdout and stderr from being printed to your terminal.
  • nothrow(): Do not throw an error on non-zero exit codes; instead, return the result.
  • throws(true/false): Control whether non-zero exit codes result in an error being thrown.
  • env(): Set environment variables for the command.
  • cwd(): Set a custom working directory.
  • timeout(): Set a timeout after which the command will be killed if not completed.

Direct Awaiting

You can await the shell command directly, which resolves to a ShellOutput object:

const output = await $`echo hello`.quiet();
console.log(output.stdout.toString()); // "hello\n"

API

Template Tag: $

A tagged template function to execute shell commands.

Example:

const result = await $`ls -l`.text();
console.log(result);

Methods

  • text(): Resolves with the command's stdout as a string.
  • json(): Resolves with the command's stdout as a parsed JSON object.
  • arrayBuffer(): Resolves with stdout as an ArrayBuffer.
  • blob(): Resolves with stdout as a Blob.
  • lines(): Returns an async iterable that yields each line of stdout as it arrives.
  • env(env: Record<string, string>): Sets environment variables for the command.
  • quiet(): Suppresses stdout and stderr output.
  • nothrow(): Prevents errors from being thrown on non-zero exit codes.
  • throws(boolean): Control error throwing behavior based on exit codes.
  • cwd(path: string): Change the command's working directory.
  • timeout(ms: number): Set a timeout for the command execution.

Contributing

Contributions are welcome! Feel free to open issues or pull requests if you have improvements or find bugs.