@farm.js/sentry

中文文档
0.1.48发布于 2026/11/01MITTypeScriptES Module

Sentry error reporting and tracing for Farm.js

原始使用文档
文档目录9 节
  1. Install
  2. Configure
  3. Initialize early
  4. What it does
  5. Serverless
  6. Options
  7. Source maps
  8. Runtime support
  9. Bring your own SDK

@farm.js/sentry

Sentry error reporting and tracing for Farm.js applications. It maps Farm's request, render and build lifecycles onto Sentry, so errors arrive with route context and requests are traced.

See Stability and Support for what this package guarantees.

Install

pnpm add @farm.js/sentry @sentry/node

@sentry/node is an optional peer dependency. If a dsn is configured and it is not installed, the plugin logs a clear error and continues without reporting rather than preventing the application from starting.

Configure

import { defineConfig } from "@farm.js/core";
import { sentryPlugin } from "@farm.js/sentry";

export default defineConfig({
  plugins: [
    sentryPlugin({
      dsn: process.env.SENTRY_DSN,
      tracesSampleRate: 0.1,
      sourceMaps: true,
    }),
  ],
});

Initialize early

The Node SDK patches other modules as they load, so it has to run before the rest of the application. Add an instrumentation file for that:

// src/instrumentation.ts
import { registerSentry } from "@farm.js/sentry";

export const register = registerSentry({
  dsn: process.env.SENTRY_DSN,
  tracesSampleRate: 0.1,
});

Without this the plugin still reports errors, but the SDK's automatic database and HTTP instrumentation will not attach.

What it does

HookBehavior
runtime.startinitializes the SDK if nothing has already, and listens for error events
runtime.contextnames the request span by route pattern
runtime.aftersets the span status from the response
runtime.errorcaptures the exception with route, kind and request context
runtime.closeflushes pending events on shutdown
build.configureenables source maps when sourceMaps is set

Spans are named by route pattern rather than pathname, so /users/[id] stays one span name instead of one per user.

Where the SDK's own HTTP instrumentation has already opened a span for the request, the plugin renames that one rather than starting another, so automatic HTTP and database spans stay nested under the request. It only creates a span when nothing is active.

Serverless

Hosts that can terminate a process without a shutdown signal never run runtime.close, so pending events are lost. Flush inside the request instead:

sentryPlugin({
  dsn: process.env.SENTRY_DSN,
  flushOnResponse: true,
});

The flush is handed to waitUntil, so the host keeps the invocation alive until it completes.

Options

OptionDefaultDescription
dsnSentry project DSN.
environmentinstrumentation modeEnvironment name.
releaseRelease identifier for the deploy.
tracesSampleRateFraction of requests traced. Errors are always sent.
sendDefaultPiifalseInclude request headers and user data.
enabledtrueSet false to register the hooks but report nothing.
sdk@sentry/nodeAn SDK module to use instead of importing it.
flushOnResponsefalseFlush after every response and after a failed request.
flushTimeoutMs2000Flush timeout.
sourceMapsfalseGenerate source maps in the production build. Does not upload them, see below.

sendDefaultPii stays off by default because error events can carry request headers and user data.

Source maps

sourceMaps: true only generates source maps during the production build. It does not upload them to Sentry, so production stack traces stay minified until the maps are uploaded separately, for example with sentry-cli. Uploading from the build is not implemented yet.

Farm only uses its fast Oxc minifier while source maps are off, so enabling them moves minification to Nitro's terser:

pnpm add -D @rollup/plugin-terser

Without it the production build fails with Cannot find module '@rollup/plugin-terser'.

Runtime support

Node presets. @sentry/node does not run on Cloudflare Workers, which need @sentry/cloudflare instead, so registerSentry is a no-op on edge and bun runtimes. Edge support is tracked separately.

Bring your own SDK

sdk accepts anything matching the small structural interface the plugin uses, which is also how the package is unit tested:

import * as Sentry from "@sentry/node";

sentryPlugin({ sdk: Sentry });

版本与发布记录

@farm.js/sentry 共发布 97 个版本,首次发布于 2026/08/28;过去 12 个月发布了 97 个版本。

版本发布日期距今
0.1.48latest2026/11/012 天前
0.1.472026/10/313 天前
0.1.462026/10/313 天前
0.1.452026/10/313 天前
0.1.442026/10/313 天前
0.1.432026/10/304 天前

其他发布通道:beta → 0.1.3-beta.0。

关于 @farm.js/sentry 的常见问题

@farm.js/sentry 是什么?

为Farm.js提供错误报告和跟踪

如何安装 @farm.js/sentry?

在项目目录运行 npm install @farm.js/sentry,或使用 pnpm add / yarn add / bun add。

@farm.js/sentry 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@farm.js/sentry 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@farm.js/sentry 可以商用吗?

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

@farm.js/sentry 还在维护吗?

最新版本 0.1.48 发布于 2026/11/01(2 天前);至今共发布 97 个版本,其中过去 12 个月发布了 97 个;首次发布于 2026/08/28。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。