# graceful

> Graceful exit when `uncaughtException` emit, base on `process.on('uncaughtException')`.

Latest version **2.0.0** (published 2024-12-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install graceful
pnpm add graceful
yarn add graceful
bun add graceful
```

## Health

**Score 45/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities; has provenance; high quality score.

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2024-12-15 |
| First published | 2013-04-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >= 18.19.0 |
| Dependencies | 2 |
| Unpacked size | 33.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 250 |
| Author | fengmk2 |
| Maintainers | fengmk2 |
| Keywords | graceful, uncaught, uncaughtException, error, graceful, cluster, graceful exit |

## Links

- npm: https://www.npmjs.com/package/graceful
- Repository: https://github.com/node-modules/graceful
- Issues: https://github.com/node-modules/graceful/issues
- npm.io page: https://npm.io/package/graceful

## Dependencies (2)

- [humanize-ms](https://npm.io/package/humanize-ms.md) ^2.0.0
- [@fengmk2/ps-tree](https://npm.io/package/@fengmk2/ps-tree.md) ^2.0.2

## Alternatives

- [@sentry/react-native](https://npm.io/package/@sentry/react-native.md) — 2.6M weekly downloads
- [@ardatan/aggregate-error](https://npm.io/package/@ardatan/aggregate-error.md) — 708.1K weekly downloads
- [custom-error-generator](https://npm.io/package/custom-error-generator.md) — 2.0K weekly downloads
- [@technik-sde/prosemirror-recreate-transform](https://npm.io/package/@technik-sde/prosemirror-recreate-transform.md) — 1.5K weekly downloads
- [@suchipi/error-utils](https://npm.io/package/@suchipi/error-utils.md) — 78 weekly downloads

## Recent versions

- 2.0.0 (latest) — 2024-12-15
- 1.1.0 — 2022-09-22
- 1.0.2 — 2018-10-31
- 1.0.1 — 2016-06-23
- 1.0.0 — 2014-11-05
- 0.1.0 — 2014-05-29
- 0.0.6 — 2014-02-17
- 0.0.5 — 2013-04-18
- 0.0.4 — 2013-04-18
- 0.0.3 — 2013-04-14
- 0.0.2 — 2013-04-14
- 0.0.1 — 2013-04-12

## README

# graceful

[![NPM version][npm-image]][npm-url]
[![Test coverage][cov-image]][cov-url]
[![npm download][download-image]][download-url]
[![Node.js Version](https://img.shields.io/node/v/graceful.svg?style=flat)](https://nodejs.org/en/download/)

[npm-image]: https://img.shields.io/npm/v/graceful.svg?style=flat-square
[npm-url]: https://npmjs.org/package/graceful
[cov-image]: https://codecov.io/github/node-modules/graceful/coverage.svg?branch=master
[cov-url]: https://codecov.io/github/node-modules/graceful?branch=master
[download-image]: https://img.shields.io/npm/dm/graceful.svg?style=flat-square
[download-url]: https://npmjs.org/package/graceful

Graceful exit when `uncaughtException` emit, base on `process.on('uncaughtException')`.

## Why we should use this module

It's the best way to handle `uncaughtException` on current situations.

* [Node.js 异步异常的处理与domain模块解析](http://deadhorse.me/nodejs/2013/04/13/exception_and_domain.html)

## Install

```bash
npm install graceful
```

## Usage

Please see [express_with_cluster](https://github.com/node-modules/graceful/tree/master/example/express_with_cluster) example.

This below code just for dev demo, don't use it on production env:

```js
const express = require('express');
const { graceful } = require('graceful');

const app = express()
.use()
.use(function(req, res){
  if (Math.random() > 0.5) {
    foo.bar();
  }
  setTimeout(function() {
    if (Math.random() > 0.5) {
      throw new Error('Asynchronous error from timeout');
    } else {
      res.end('Hello from Connect!');
    }
  }, 100);
  setTimeout(function() {
    if (Math.random() > 0.5) {
      throw new Error('Mock second error');
    }
  }, 200);
})
.use(function(err, req, res, next) {
  res.end(err.message);
});

const server = app.listen(1984);

graceful({
  servers: [server],
  killTimeout: '30s',
});
```

If you have multi servers on one process, you just add them to `server`:

```js
graceful({
  servers: [server1, server2, restapi],
  killTimeout: '15s',
});
```

### ESM and TypeScript

```ts
import { graceful } from 'graceful';
```

## Contributors

[![Contributors](https://contrib.rocks/image?repo=node-modules/graceful)](https://github.com/node-modules/graceful/graphs/contributors)

Made with [contributors-img](https://contrib.rocks).

## License

[MIT](LICENSE)

---
_Source: https://npm.io/package/graceful · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
