Skip to main content

Node.js

Beta

This page documents the Bugsee JavaScript SDK, which is in beta (0.1.x) and can change between betas.

On Node.js, @bugsee/bugsee captures uncaught exceptions and unhandled rejections, console output and node:http traffic, instruments incoming requests, and keeps its recording on disk so a crash report survives the crash. It can also detect event-loop hangs and attach a CPU profile.

Running an HTTP framework? Start here, then add its adapter from Backend.

Requirements​

  • Node.js 18 or newer.
  • An app token. Create a JavaScript application in Bugsee and choose Node.js as its target; see Create your app.
  • A writable directory for the on-disk capture buffer. By default it is <os tmpdir>/bugsee.

Install​

npm install @bugsee/bugsee

You install only @bugsee/bugsee. It selects the Node build through package exports conditions, for both ESM and CommonJS. @bugsee/node is an internal building block that you do not install.

Launch​

Call launch() at the top of your entry file, before the code you want to observe.

server.ts
import { launch } from '@bugsee/bugsee';

const bugsee = launch('<your_app_token>', { appVersion: '1.4.2' });

On Node launch() returns the client directly, not a Promise. A second launch() in the same process is ignored with a warning and returns the existing client. Use relaunch(options?) to restart, or stop() to release the singleton. A missing or empty token does not throw: the SDK reports one error through onError (or console.warn) and returns a disabled client.

Read the token from your environment rather than hard-coding it:

launch(process.env.BUGSEE_APP_TOKEN ?? '');

App version and build​

The SDK picks the first usable value in this order:

  1. The appVersion and appBuild launch options.
  2. Build-time constants __BUGSEE_APP_VERSION__ and __BUGSEE_APP_BUILD__.
  3. The appIdentitySource option, a function returning { version?, build? }.
  4. For the version: the BUGSEE_APP_VERSION environment variable, then the version field of the nearest package.json, searched upward from appRoot or the working directory. If the working directory has none (a container with WORKDIR /, or a service manager's folder), the folder of the entry script is searched next (Bun.main on Bun, otherwise process.argv[1]). An entry script under node_modules, which is a tool's script and not your app, is not searched, and an explicit appRoot is never overridden.
  5. For the build: the BUGSEE_APP_BUILD environment variable, then the commit variable of your CI or host, such as GITHUB_SHA or VERCEL_GIT_COMMIT_SHA.

If no version is found, reports show 0.0.0 and the SDK prints one warning.

What is exported​

ExportDescription
launch(appToken, options?)Starts the SDK and returns the client synchronously.
relaunch(options?)Restarts the SDK, optionally with new options.
getStatus()Returns the current BugseeStatus.
BugseeStatusThe status values getStatus() returns.
LifecycleEventsThe lifecycle event names. See Lifecycle.
addReportAttachment(input)Adds an attachment to reports.
getDefaultNetworkSanitizer()The default network redaction, to compose with your own filter.

Node options​

launch(appToken, options) accepts the common options. These apply to Node (and Bun):

OptionTypeDefaultDescription
appRootstringworking directoryWhere to start looking for package.json.
unhandledRejections'preserve' | 'warn' | 'none''preserve'preserve captures the rejection, then reproduces Node's outcome (print and exit 1). warn captures and prints but keeps the process alive. none installs no listener.
exitOnUncaughtbooleantrueCall process.exit(1) after flushing an uncaught exception. Does not affect rejections.
shutdownTimeoutMsnumber3000Time budget to flush the crash report before exiting.
recoverbooleantruePersist each report before upload and re-upload leftovers from a crashed run on the next launch.
capturedDataStore'disk' | 'memory''disk'Where the recording lives. memory disables crash recovery.
dataDirstring<os tmpdir>/bugseeDirectory for the on-disk buffer.
maxDataSizenumber50 MBSize cap of the recording.
detectHangsbooleantrueEvent-loop hang detection. See Hangs.
profilingbooleanfalseRolling V8 CPU profile attached to reports.
captureLocalVariablesboolean or objectfalseLocal variables of the throwing frames. See Diagnostics.
captureSourceContextboolean or objectfalseSource lines around each frame.
instrumentIncomingRequestsbooleantrueInstrument incoming node:http requests automatically.

The remaining Node options (deviceId, captureWriter, captureDiskSpace, diskProtection, trace propagation) are in Configuration.

Behavior to know about​

  • Crashes end the process. After an uncaught exception the SDK flushes the report and exits with code 1, as Node would. Set exitOnUncaught: false only if you handle process lifetime yourself.
  • Crash recovery needs a persistent directory. If the process is killed, the next launch uploads what the previous run left in dataDir. On a read-only or ephemeral filesystem, set dataDir to a writable path or accept in-memory capture.
  • Several processes or worker threads can share one dataDir. Each instance writes its own subdirectory, and a new launch recovers the data of dead siblings.
  • Incoming requests are instrumented by default. Each request gets its own context so logs and errors can be tied to it. See Backend.
  • Serverless and short-lived scripts. Call await bugsee.flush() before the process ends so pending uploads finish.

Source maps​

If you ship bundled or transpiled server code, upload source maps so stack traces are readable. See Symbolication.

Verify​

  1. Launch the SDK with a real token.
  2. Report a test error:
const bugsee = launch('<your_app_token>');
bugsee.logException(new Error('Bugsee test error'));
await bugsee.flush();
  1. Open the Bugsee dashboard. The report appears within a minute, with the process environment and the captured logs.
  • Quick start — install and launch for every runtime
  • Configuration — all launch options
  • Backend — request context and framework adapters
  • Hangs — event-loop hang detection
  • Bun — the Bun runtime
Found an issue, typo, or wrong statement on this page? Report it now →