Skip to content

Instantly share code, notes, and snippets.

@MirzaLeka
Last active November 11, 2025 08:46
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.

@mozeryansky
Copy link

mozeryansky commented Apr 5, 2025

@dalalRohit It speeds up local builds when running npm run dev significantly. ymmv

@nil-andreu
Copy link

Thanks for this @mozeryansky !

const nextConfig: NextConfig = {
  ...
}

if (process.env.SENTRY === 'true' || process.env.NODE_ENV === 'production') {
  const { withSentryConfig } = require('@sentry/nextjs')

  config = withSentryConfig(config, {
    ...
  })

  module.exports = {
    ...config,
    serverExternalPackages: ['@sentry/profiling-node'],
    async rewrites() {
      return await nextConfig.rewrites()
    },
  }
} else {
  module.exports = config
}

Btw, with the linter i was having an issue that A require() style import is forbidden. As the require() is necessary because Next.js config files are executed in CommonJS context, I added this comment to disable the check:

  // eslint-disable-next-line @typescript-eslint/no-require-imports
  const { withSentryConfig } = require('@sentry/nextjs');

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