Skip to content

Instantly share code, notes, and snippets.

@MirzaLeka
Last active June 2, 2024 00:19
Show Gist options
  • Save MirzaLeka/d709d0925d9b749d1b9068abf4b5ad50 to your computer and use it in GitHub Desktop.
Save MirzaLeka/d709d0925d9b749d1b9068abf4b5ad50 to your computer and use it in GitHub Desktop.
Disable Sentry in development environment (or any other env) using Next.js/Node.js

Disable Sentry in development environment (or any other)

Look for Sentry config file/s (sentry.client.js & sentry.config.js)

Either file should contain an object like this

Sentry.init({
  dsn: DSN_YOU_ACQUIRED_FROM_SENTRY
})

To disable Sentry in any env you can make use of enabled flag in Sentry config and process.env.NODE_ENV Node.js environment variable. This variable is built into Node.js and is set to 'development' or 'production' depending on your app environment.

To disable in development, do the following;

Sentry.init({
  dsn: DSN_YOU_ACQUIRED_FROM_SENTRY,
  enabled: process.env.NODE_ENV === 'production'
})

Now Sentry will only be active in Production environment.

Read more on Sentry configuration in the official Docs.

@ADTC
Copy link

ADTC commented Dec 28, 2023

I'm using Sentry in Next.js. How to do this in it? There's no enabled option in either of the two JSON blocks (as per Intellisense suggestions).

const { withSentryConfig } = require('@sentry/nextjs')

module.exports = withSentryConfig(
  module.exports,
  {
    // For all available options, see:
    // https://github.com/getsentry/sentry-webpack-plugin#options

    // Suppresses source map uploading logs during build
    silent: true,

    org: 'my-org',
    project: 'my-project',
  },
  {
    // For all available options, see:
    // https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/

    // Upload a larger set of source maps for prettier stack traces (increases build time)
    widenClientFileUpload: true,

    // Transpiles SDK to be compatible with IE11 (increases bundle size)
    transpileClientSDK: true,

    // Routes browser requests to Sentry through a Next.js rewrite to circumvent ad-blockers (increases server load)
    tunnelRoute: '/monitoring',

    // Hides source maps from generated client bundles
    hideSourceMaps: true,

    // Automatically tree-shake Sentry logger statements to reduce bundle size
    disableLogger: true,
  },
)

@ADTC
Copy link

ADTC commented Dec 28, 2023

Never mind, it looks like I have to search for Sentry.init in the project to find it, then set the option in all the instances there.

@MirzaLeka
Copy link
Author

@ADTC
I'm glad it worked out for you!

@ivanlen
Copy link

ivanlen commented Feb 28, 2024

Thanks 🙌

@mozeryansky
Copy link

I wanted to skip compiling /instrumentation.ts (which is what calls Sentry.init) to reduce the next dev startup time, so I disable it by turning it off from the next config:

const nextConfig = {
   ...
}

let config = withBundleAnalyzer(nextConfig)

config = withSentryConfig(config, {
    ...
}

module.exports = {
  ...config,
  experimental: {
    instrumentationHook: process.env.NODE_ENV === 'production',
  },
}

I had to lay it out like this as I had custom rewrites and webpack, which withSentryConfig will modify so it must be done in this order to preserve all the settings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment