Node.js Node.js logo integration with BugSplat crash reporting

BugSplat’s JavaScript integration works with any application that supports npm including Node.js, and Electron applications.



To add the bugsplat package to your application, run the following shell command at the root of your project’s directory:

npm install --save bugsplat

Require the bugsplat module at the entry point of your application. Provide the name of your BugSplat database, the name of your application and the version of your application:

const bugsplat = require("bugsplat")("DatabaseName", "AppName", "");

Set the function as an event handler for uncaught exceptions:

process.on("uncaughtException", bugsplat.postAndExit);

If your application uses promises you will also want to listen for unhandled promise rejections. Please note that this will only work for native promises:

process.on("unhandledRejection", bugsplat.postAndExit);

Throw an exception after the event handler has been added:

throw new Error("BugSplat!");

You can also use bugsplat-js to post errors from non-fatal promise rejections and errors that originate inside of try-catch blocks:

Promise.reject(new Error("BugSplat!"))
    .catch(err =>, {}, callback));
try {
  throw new Error("BugSplat!");
} catch(err) {, {}, callback));

After posting an error with bugsplat-js, navigate to the Crashes page in BugSplat and you should see a new crash report for the application you just configured. Click the link in the Id column to see details about your crash on the Individual Crash page:

Integrating Node.js with BugSplat Integrating Node.js with BugSplat

That’s it! Your application is now configured to post crash reports to BugSplat.



In addition to the configuration demonstrated above, there are a few public methods that can be used to customize your BugSplat integration:

bugsplat.setDefaultAppKey(appKey); // Additional metadata that can be queried via BugSplat's web application
bugsplat.setDefaultUser(user); // The name or id of your user
bugsplat.setDefaultEmail(email); // The email of your user 
bugsplat.setDefaultDescription(description); // Additional info about your crash that gets reset after every post
bugsplat.addAdditionalFile(pathToFile); // Path to a file to be added at post time (limit 1MB) 
bugsplat.postAndExit(error); // Wrapper for post that calls process.exit(1) after posting error to BugSplat; // Posts an arbitrary Error object to BugSplat
// If the values options.appKey, options.user, or options.description are set the corresponding default values will be overwritten
// The callback is a function that accepts 3 parameters requestError, responseBody, originalError where orignalError is the error that was posted to BugSplat

Additional Considerations

It is recommended that you exit and restart your application after an uncaughtException or unhandledRejection occurs. Packages such as pm2 and forever can be configured to restart your application.

Additionally you can use domains to handle errors differently across various parts of your application. Domains are pending deprecation according the the Node.js documentation, however a suitable replacement has not been added yet.

More information regarding domain deprecation can be found here.



BugSplat loves open source software! Please check out our project on GitHub and send us a pull request.