3

In my express app I initialise Sentry as follows.

const Sentry = require('@sentry/node');
const Tracing = require('@sentry/tracing');

const app = express();
const server = require('http').createServer(app);


Sentry.init({
    dsn:
        '...',
    integrations: [
        // enable HTTP calls tracing
        new Sentry.Integrations.Http({ tracing: true }),
        // enable Express.js middleware tracing
        new Tracing.Integrations.Express({ app }),
    ],

    // Set tracesSampleRate to 1.0 to capture 100%
    // of transactions for performance monitoring.
    // We recommend adjusting this value in production
    tracesSampleRate: 0.8,
});

app.use(Sentry.Handlers.requestHandler());
// TracingHandler creates a trace for every incoming request
app.use(Sentry.Handlers.tracingHandler());

server.listen(PORT);

Next, I export Sentry as follows from this file:

module.exports = {
    app,
    Sentry,
};

In other files, I would like to use the captureException method to manually report errors to Sentry. In these files I have:

const { Sentry } = require('./path/to/above/file');

Sentry.captureException(ex);

This throws the error to Sentry: cannot read property captureException of undefined which to me sounds like there's an issue with my export / import logic leading to Sentry being undefined, but I don't understand what the issue could be.

5
  • Are you sure you're requiring the right file? What is the output of console.log(require('./path/to/above/file')) ?
    – Andy Ray
    Commented May 12, 2021 at 21:14
  • Could it be a circular dependency? I've seen issues like that in the past with Node when you reference an object from a file in another file, and vice versa and it says its undefined
    – Alk
    Commented May 12, 2021 at 21:15
  • Do I need to use the initialised Sentry object when I want to call captureException or can I just do const Sentry = require('@sentry/node'); in those places instead?
    – Alk
    Commented May 12, 2021 at 21:17
  • 4
    Doing this resolves the issue. import * as Sentry from '@sentry/node';
    – Ukor
    Commented Dec 23, 2021 at 12:51
  • the comment by @Ukor should be an answer, an accepted one. Thanks!
    – billomore
    Commented Nov 20, 2022 at 14:34

1 Answer 1

0

Reposting the comment of @Ukor:

Doing this resolves the issue:

import * as Sentry from '@sentry/node';

I also faced the same issue in my react native project and in my case, I was mistakenly importing

import Sentry from "@sentry/react-native

Replacing it with

import * as Sentry from "@sentry/react-native";

resolved the issue.

Not the answer you're looking for? Browse other questions tagged or ask your own question.