Setting Up Stackdriver Error Reporting for Node.js

You can send error reports to Stackdriver Error Reporting from Node.js applications by using the Stackdriver Error Reporting library for Node.js.

However, some products such as Cloud Functions and App Engine have integrations with Stackdriver Error Reporting such that errors logged to Stackdriver Logging are automatically processed and displayed by Stackdriver Error Reporting. See Running on Google Cloud Platform for more information.

Installing the library

We provide a Stackdriver Error Reporting library for Node.js, which enables in-depth monitoring and viewing of errors reported by Node.js applications running in almost any environment.

  1. Before installing the plugin, make sure you've prepared your environment for Node.js development.

  2. The easiest way to install the Stackdriver Error Reporting library for Node.js is with npm:

    npm install --save @google-cloud/error-reporting

  3. Import the library and instantiate a client in order to begin reporting errors:

    // Imports the Google Cloud client library
    const ErrorReporting = require('@google-cloud/error-reporting');
    
    // Instantiates a client
    const errors = ErrorReporting();
    
    // Reports a simple error
    errors.report('Something broke!');

For more information or to report issues with the library, see the library's source code repository.

Configuring the client

You can customize the behavior of the Stackdriver Error Reporting library for Node.js. See the library's configuration for a list of possible configuration options. These options can be passed in the options object passed to the library's constructor.

Reporting errors manually

Manually reporting an error is as easy as calling the report method:

// Imports the Google Cloud client library
const ErrorReporting = require('@google-cloud/error-reporting');

// Instantiates a client
const errors = ErrorReporting();

// Use the error message builder to customize all fields ...
const errorEvent = errors.event();

// Add error information
errorEvent.setMessage('My error message');
errorEvent.setUser('root@nexus');

// Report the error event
errors.report(errorEvent, () => {
  console.log('Done reporting error event!');
});

// Report an Error object
errors.report(new Error('My error message'), () => {
  console.log('Done reporting Error object!');
});

// Report an error by provided just a string
errors.report('My error message', () => {
  console.log('Done reporting error string!');
});

Reporting errors with Express.js

The Stackdriver Error Reporting library for Node.js makes it easy to integrate Stackdriver Error Reporting into popular Node.js web frameworks such as Express.js:

const express = require('express');

// Imports the Google Cloud client library
const ErrorReporting = require('@google-cloud/error-reporting');

// Instantiates a client
const errors = ErrorReporting();

const app = express();

app.get('/error', (req, res, next) => {
  res.send('Something broke!');
  next(new Error('Custom error message'));
});

app.get('/exception', () => {
  JSON.parse('{"malformedJson": true');
});

// Note that express error handling middleware should be attached after all
// the other routes and use() calls. See the Express.js docs.
app.use(errors.express);

const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
  console.log(`App listening on port ${PORT}`);
  console.log('Press Ctrl+C to quit.');
});

Other integrations

For more information on integrations between the Stackdriver Error Reporting library for Node.js and other Node.js web frameworks, see the library's repository on GitHub.

Running on Google Cloud Platform

The Stackdriver Error Reporting library for Node.js should work without the need to manually provide authentication credentials for instances running on Google Cloud Platform, as long as the Stackdriver Error Reporting API access scope is enabled on that instance.

Cloud Functions

On Google Cloud Functions, the Stackdriver Error Reporting API access scope is enabled by default, and the Stackdriver Error Reporting library for Node.js can be used without needing to provide credentials or a project ID.

However, Cloud Functions is configured to use Stackdriver Error Reporting automatically. Unhandled JavaScript exceptions will appear in Stackdriver Logging and be processed by Stackdriver Error Reporting without the need to use the Stackdriver Error Reporting library for Node.js.

App Engine

On Google App Engine, the Stackdriver Error Reporting API access scope is enabled by default, and the Stackdriver Error Reporting library for Node.js can be used without needing to provide credentials or a project ID.

However, Stackdriver Error Reporting is automatically enabled for App Engine flexible environment applications. No additional setup is required. Errors written to stderr, which App Engine writes to, are analyzed and displayed by Stackdriver Error Reporting.

Container Engine

On Google Container Engine, you need to explicitly add the cloud-platform OAuth scope when creating the cluster:

gcloud container clusters create example-cluster-name --scopes https://www.googleapis.com/auth/cloud-platform

Compute Engine

For Google Compute Engine instances, you need to explicitly enable the cloud-platform access scope for each instance. When creating a new instance through the Google Cloud Platform Console, you can do this under Identity and API access: Use the Compute Engine default service account and select "Allow full access to all Cloud APIs" under Access scopes.

To use something other than the Compute Engine default service account see the docs for Creating and Enabling Service Accounts for Instances and the Running elsewhere section below. The important thing is that the service account you use has the Error Reporting Writer role.

Running locally and elsewhere

If your application is running outside of Google Cloud Platform, such as locally, on-premise, or on another cloud provider, you can still use Stackdriver Error Reporting, provided that you supply a project ID and credentials to the Stackdriver Error Reporting library for Node.js, as described in the following sections.

Project ID

Provide a project ID to the Stackdriver Error Reporting library for Node.js by setting the projectId value in the options object passed to the libraries' constructors, or set the GCLOUD_PROJECT environment variable:

Linux or Mac OS X

export GCLOUD_PROJECT=your-project-id

Windows

set GCLOUD_PROJECT=your-project-id

Powershell:

$env:GCLOUD_PROJECT="your-project-id"

Credentials

The Stackdriver Error Reporting library for Node.js use a strategy called Application Default Credentials (ADC) to find your application's credentials. You can provide credentials to the Stackdriver Error Reporting library for Node.js by setting the keyFilename value in the options object passed to the libraries' constructors, or you can set the GOOGLE_APPLICATION_CREDENTIALS environment variable. For more information, see Providing credentials to your application.

Example

// Imports the Google Cloud client library
const ErrorReporting = require('@google-cloud/error-reporting');

// Instantiates a client
const errors = ErrorReporting({
  projectId: 'your-project-id',
  keyFilename: '/path/to/key.json'
});

// Reports a simple error
errors.report('Something broke!');

Viewing the error reports

After deployment, you can view error reports in the Cloud Platform Console Errors Viewer.

Go to the Errors Viewer page

Send feedback about...

Stackdriver Error Reporting