Node.js
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
- pnpm
- yarn
- bun
npm install @bugsee/bugsee
pnpm add @bugsee/bugsee
yarn add @bugsee/bugsee
bun add @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.
- TypeScript
- CommonJS
import { launch } from '@bugsee/bugsee';
const bugsee = launch('<your_app_token>', { appVersion: '1.4.2' });
const { launch } = require('@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:
- The
appVersionandappBuildlaunch options. - Build-time constants
__BUGSEE_APP_VERSION__and__BUGSEE_APP_BUILD__. - The
appIdentitySourceoption, a function returning{ version?, build? }. - For the version: the
BUGSEE_APP_VERSIONenvironment variable, then theversionfield of the nearestpackage.json, searched upward fromappRootor the working directory. If the working directory has none (a container withWORKDIR /, or a service manager's folder), the folder of the entry script is searched next (Bun.mainon Bun, otherwiseprocess.argv[1]). An entry script undernode_modules, which is a tool's script and not your app, is not searched, and an explicitappRootis never overridden. - For the build: the
BUGSEE_APP_BUILDenvironment variable, then the commit variable of your CI or host, such asGITHUB_SHAorVERCEL_GIT_COMMIT_SHA.
If no version is found, reports show 0.0.0 and the SDK prints one warning.
What is exported
| Export | Description |
|---|---|
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. |
BugseeStatus | The status values getStatus() returns. |
LifecycleEvents | The 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):
| Option | Type | Default | Description |
|---|---|---|---|
appRoot | string | working directory | Where 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. |
exitOnUncaught | boolean | true | Call process.exit(1) after flushing an uncaught exception. Does not affect rejections. |
shutdownTimeoutMs | number | 3000 | Time budget to flush the crash report before exiting. |
recover | boolean | true | Persist 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. |
dataDir | string | <os tmpdir>/bugsee | Directory for the on-disk buffer. |
maxDataSize | number | 50 MB | Size cap of the recording. |
detectHangs | boolean | true | Event-loop hang detection. See Hangs. |
profiling | boolean | false | Rolling V8 CPU profile attached to reports. |
captureLocalVariables | boolean or object | false | Local variables of the throwing frames. See Diagnostics. |
captureSourceContext | boolean or object | false | Source lines around each frame. |
instrumentIncomingRequests | boolean | true | Instrument 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: falseonly 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, setdataDirto 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
- Launch the SDK with a real token.
- Report a test error:
const bugsee = launch('<your_app_token>');
bugsee.logException(new Error('Bugsee test error'));
await bugsee.flush();
- Open the Bugsee dashboard. The report appears within a minute, with the process environment and the captured logs.
Related pages
- 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