@airbrake/node

中文文档
2.1.9发布于 2025/03/13MITESM + CJS近 30 天下载 9.5万

Official Airbrake notifier for Node.js

原始使用文档
文档目录11 节
  1. Installation
  2. Basic Usage
  3. Example configurations
  4. Advanced Usage
  5. Notice Annotations
  6. Severity
  7. Filtering errors
  8. Filtering keys
  9. Node.js request and proxy
  10. Instrumentation
  11. Filtering performance data

Official Airbrake Notifier for Node.js

Build Status npm version npm dm npm dt

The official Airbrake notifier for capturing JavaScript errors in Node.js and reporting them to Airbrake. If you're looking for browser support, there is a separate package.

Installation

Using yarn:

yarn add @airbrake/node

Using npm:

npm install @airbrake/node

Basic Usage

First, initialize the notifier with the project ID and project key taken from Airbrake. To find your project_id and project_key navigate to your project's Settings and copy the values from the right sidebar:

const { Notifier } = require('@airbrake/node');

const airbrake = new Notifier({
  projectId: 1,
  projectKey: 'REPLACE_ME',
  environment: 'production',
});

Then, you can send a textual message to Airbrake:

let promise = airbrake.notify(`user id=${user_id} not found`);
promise.then((notice) => {
  if (notice.id) {
    console.log('notice id', notice.id);
  } else {
    console.log('notify failed', notice.error);
  }
});

or report errors directly:

try {
  throw new Error('Hello from Airbrake!');
} catch (err) {
  airbrake.notify(err);
}

Alternatively, you can wrap any code which may throw errors using the wrap method:

let startApp = () => {
  throw new Error('Hello from Airbrake!');
};
startApp = airbrake.wrap(startApp);

// Any exceptions thrown in startApp will be reported to Airbrake.
startApp();

or use the call shortcut:

let startApp = () => {
  throw new Error('Hello from Airbrake!');
};

airbrake.call(startApp);

Example configurations

Advanced Usage

Notice Annotations

It's possible to annotate error notices with all sorts of useful information at the time they're captured by supplying it in the object being reported.

try {
  startApp();
} catch (err) {
  airbrake.notify({
    error: err,
    context: { component: 'bootstrap' },
    environment: { env1: 'value' },
    params: { param1: 'value' },
    session: { session1: 'value' },
  });
}

Severity

Severity allows categorizing how severe an error is. By default, it's set to error. To redefine severity, simply overwrite context/severity of a notice object:

airbrake.notify({
  error: err,
  context: { severity: 'warning' },
});

Filtering errors

There may be some errors thrown in your application that you're not interested in sending to Airbrake, such as errors thrown by 3rd-party libraries.

The Airbrake notifier makes it simple to ignore this chaff while still processing legitimate errors. Add filters to the notifier by providing filter functions to addFilter.

addFilter accepts the entire error notice to be sent to Airbrake and provides access to the context, environment, params, and session properties. It also includes the single-element errors array with its backtrace property and associated backtrace lines.

The return value of the filter function determines whether or not the error notice will be submitted.

  • If null is returned, the notice is ignored.
  • Otherwise, the returned notice will be submitted.

An error notice must pass all provided filters to be submitted.

In the following example all errors triggered by admins will be ignored:

airbrake.addFilter((notice) => {
  if (notice.params.admin) {
    // Ignore errors from admin sessions.
    return null;
  }
  return notice;
});

Filters can be also used to modify notice payload, e.g. to set the environment and application version:

airbrake.addFilter((notice) => {
  notice.context.environment = 'production';
  notice.context.version = '1.2.3';
  return notice;
});

Filtering keys

With the keysBlocklist option, you can specify a list of keys containing sensitive information that must be filtered out:

const airbrake = new Notifier({
  // ...
  keysBlocklist: [
    'password', // exact match
    /secret/, // regexp match
  ],
});

Node.js request and proxy

To use the request HTTP client, pass the request option which accepts a request wrapper:

const airbrake = new Notifier({
  // ...
  request: request.defaults({ proxy: 'http://localproxy.com' }),
});

Instrumentation

@airbrake/node attempts to automatically instrument various performance metrics. You can disable that behavior using the performanceStats option:

const airbrake = new Notifier({
  // ...
  performanceStats: false,
});

Filtering performance data

addPerformanceFilter allows for filtering performance data. Return null in the filter to prevent that metric from being reported to Airbrake.

airbrake.addPerformanceFilter((metric) => {
  if (metric.route === '/foo') {
    // Requests to '/foo' will not be reported
    return null;
  }
  return metric;
});

版本与发布记录

@airbrake/node 共发布 41 个版本,首次发布于 2019/08/20;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.1.9latest2025/03/131.7 年前
2.1.82022/12/063 年前
2.1.72021/10/045 年前
2.1.62021/10/045 年前
2.1.52021/06/025 年前
2.1.42021/04/165 年前

关于 @airbrake/node 的常见问题

@airbrake/node 是什么?

Official Airbrake notifier for Node.js

如何安装 @airbrake/node?

在项目目录运行 npm install @airbrake/node,或使用 pnpm add / yarn add / bun add。

@airbrake/node 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/airbrake__node 是否存在。

@airbrake/node 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@airbrake/node 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=10。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@airbrake/node 可以商用吗?

@airbrake/node 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@airbrake/node 的下载量有多少?

根据 npm 官方下载统计,@airbrake/node 在 2026/10/05 – 2026/11/03 这 30 天内被下载 95,399 次,日均约 3,180 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@airbrake/node 还在维护吗?

最新版本 2.1.9 发布于 2025/03/13(1.7 年前);至今共发布 41 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/08/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。