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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@simbo/graceful-exit

v1.1.0

Published

Gracefully terminate a Node.js process with predictable exit codes, clear console output, and optional teardown steps.

Downloads

18

Readme

Graceful Exit

📦 @simbo/graceful-exit

Gracefully terminate a Node.js process with predictable exit codes, clear console output, and optional teardown steps.

It distinguishes between numeric exit codes, user‑initiated cancellations (e.g., Inquirer’s ExitPromptError), user‑facing errors with hints, and unknown errors.

Features

  • Simple API: gracefulExit(error?, exitCode?) returns Promise<never> and exits the process.

  • 🧹 Teardown support: Run any number of sync/async cleanup steps before exiting.

  • 🧭 Smart exit codes:

    • Uses the explicit exitCode argument if provided.
    • Else, if error is a non‑zero number, uses that.
    • Else, 1 when exiting due to an error, 0 when clean.
  • 🗣️ User‑friendly output: Pretty console messages via @simbo/cli-output helpers.

  • 🙋 Special cases handled:

Installation

Install @simbo/graceful-exit from the npm registry:

npm i [-D] @simbo/graceful-exit

Usage

For a complete API reference, see the documentation.

Basic

import { gracefulExit } from '@simbo/graceful-exit';

try {
  // Your code …
  if (shouldExitEarly) {
    await gracefulExit(); // exits with code 0
  }

  // … more work

  await gracefulExit(); // exits with code 0
} catch (error) {
  await gracefulExit(error); // exits with code 1
}

With explicit exit code

await gracefulExit(new Error('Fatal'), 2); // prints error, exits with code 2
await gracefulExit(undefined, 2); // no output, exits with code 2

With teardown steps

Register cleanup steps that run before the process exits.

import { gracefulExit } from '@simbo/graceful-exit';
import {
  onTeardown,
  offTeardown,
  type TeardownStep,
} from '@simbo/graceful-exit/teardown';

// Teardown may be sync or async:
const closeDatabase: TeardownStep = async () => database.close();

const trackExit: TeardownStep = async (error?: unknown, code?: number) =>
  (error || code) && (await metrics.trackExit(error, code));

const sayGoodbye: TeardownStep = () => console.log('Goodbye!');

// Register the teardown steps:
onTeardown(closeDatabase);
onTeardown(trackExit);
onTeardown(sayGoodbye);

// Now steps will be called on any graceful exit.

try {
  // …work
  // Optionally remove a teardown step:
  if (condition) {
    offTeardown(trackExit);
  }
  await gracefulExit();
} catch (error) {
  await gracefulExit(error);
}

Steps are stored in a Set and executed in registration order.

[!NOTE]
If any teardown step fails during an otherwise successful exit (code 0), the exit code is switched to 1 and the error is logged.

Error type handling

await gracefulExit(2);
// → prints "Exiting. (Error #2)" and exits with 2

await gracefulExit(new Error('Oops'));
// → pretty‑printed via stringifyError

import { UserFacingError } from '@simbo/user-facing-error';
await gracefulExit(
  new UserFacingError('Invalid input', 'Use --help for usage'),
);
// → prints message + hint

// Inquirer cancellation (ExitPromptError)
import { ExitPromptError } from '@inquirer/core';
const e = new ExitPromptError();
await gracefulExit(e);
// → prints "Prompt Cancelled" and exits with 1

Related Packages

License

MIT © Simon Lepel