Initial Setup with Jest & TypeScript [JavaScript]

Sentry is Error Tracking Service which provides cloud service and OSS. This post describes how to setup Sentry(Raven) with TypeScript/Node.js.

🎃 Install libraries

yarn add raven dotenv

# For development
yarn add --dev @types/raven @types/dotenv

🏀 Configure Sentry

Provide SENTRY_DSN environment variable to your application. In this case, we will provide the env variable by .env file with dotenv.


Create src/utils/Config.ts file to load environment variable.

import * as dotenv from "dotenv";
const ENV = process.env;


Create src/utils/Sentry.ts file for initializing Sentry.

import * as Raven from "raven";
import { ENVIRONMENT, SENTRY_DSN } from "./Config";

export async function setUpSentry() {
if (ENVIRONMENT !== "development") {
Raven.config(SENTRY_DSN, { captureUnhandledRejections: true }).install();

🎳 Activate Sentry in your code

Please write the following code to fist position of src/index.js (src/main.js) entry file.

setUpSentry().then(() => console.log('Initialize Sentry'));

🐠 Report error manually

Raven.config().install() method sets up global handler to automatically capture any uncatch exception. You can also report errors manually.

try {
} catch (e) {

🐮 Report message


🐝 Add Context

You can also add context data to the Sentry error report. It supports your tracing a error.

Raven.context(function () {
user: {
email: '',
id: '123'

🗻 Express.js Configuration

If you want to set up Sentry with Express.js/TypeScript, pease configure as follows:

export default new App();

import * as express from "express";
import * as Raven from "raven";

const app = express();

// Sentry Configuration: Before doing anything else with it
setUpSentry().then(() => console.log("Set up Sentry"));

// Sentry request handler: Must be the first middleware on the app;

app.get("/", function mainHandler(req, res) {
throw new Error("Broke!");

// Sentry error handler: Must be before any other error middleware

// Optional fallthrough error handler
app.use((err, req, res, next) => {
// The error id is attached to `res.sentry` to be returned
// and optionally displayed to the user for support.
res.statusCode = 500;
res.end(res.sentry + "\n");


🤔 More information

If you want to know more information, please see .

🚜 References

🖥 Recommended VPS Service

VULTR provides high performance cloud compute environment for you. Vultr has 15 data-centers strategically placed around the globe, you can use a VPS with 512 MB memory for just $ 2.5 / month ($ 0.004 / hour). In addition, Vultr is up to 4 times faster than the competition, so please check it => Check Benchmark Results!!