sentry-javascript icon indicating copy to clipboard operation
sentry-javascript copied to clipboard

Cannot destructure property 'RawSource' of 'require(...)' as it is undefined error only when withSentryConfig is used and a site is viewed in Next dev mode

Open philwolstenholme opened this issue 3 years ago • 47 comments

Is there an existing issue for this?

  • [X] I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues
  • [X] I have reviewed the documentation https://docs.sentry.io/
  • [X] I am using the latest SDK release https://github.com/getsentry/sentry-javascript/releases

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/nextjs

SDK Version

7.58.1

Framework Version

13.4.10

Link to Sentry event

No response

SDK Setup

next.config.js:

const cspHeaders = require('./src/utils/csp.js');

const { env: clientEnv } = require('./src/utils/public/configuration.js');

const { withSentryConfig } = require('@sentry/nextjs');
const nextBuildId = require('next-build-id');

const bundleAnalyzer = require('@next/bundle-analyzer')({
  // eslint-disable-next-line n/no-process-env
  enabled: !!process.env.BUNDLE_ANALYSE
});

//This import allows for build time checking of the public environment variables
clientEnv;

const securityHeaders = [
  {
    key: 'X-DNS-Prefetch-Control',
    value: 'on'
  },
  {
    key: 'Strict-Transport-Security',
    value: 'max-age=63072000; includeSubDomains; preload'
  },
  {
    key: 'X-XSS-Protection',
    value: '1; mode=block'
  },
  {
    key: 'X-Frame-Options',
    value: 'SAMEORIGIN'
  },
  {
    key: 'X-Content-Type-Options',
    value: 'nosniff'
  },
  {
    key: 'Referrer-Policy',
    value: 'no-referrer-when-downgrade'
  }
];

const allPathsHeaders = [
  {
    key: 'X-Robots-Tag',
    value: 'noindex'
  },
  {
    key: 'Content-Security-Policy',
    value: cspHeaders
  }
];

/** @type {import('next').NextConfig} */
const nextConfig = {
  // We want prod sourcemaps for Rollbar/Sentry etc.
  productionBrowserSourceMaps: true,
  // Imperva's CDN will give us Brotli or Gzip compression, so this can take the load off the Next server:
  // @TODO: set this back to false when https://github.com/vercel/next.js/issues/48713 resolved
  compress: true,
  experimental: {
    // Disabled for now as experimental and https://github.com/vercel/next.js/issues/51286
    serverActions: false
  },
  images: {
    // Set a minimum cache TTL for images as the Azure blob storage product
    // image responses do not include a cache-control header
    minimumCacheTTL: 86400,
    remotePatterns: [
      {
        protocol: 'https',
        hostname: 'images.ctfassets.net'
      },
      {
        protocol: 'https',
        hostname: 'rt-1-pr-euw-retailapis-end-01.azureedge.net'
      },
      {
        protocol: 'https',
        hostname: 'rt-1-dv-euw-retailapis-end-01.azureedge.net'
      },
      {
        protocol: 'https',
        hostname: 'sspayinaisledevsst02.blob.core.windows.net'
      }
    ]
  },
  headers: async () => {
    const headers = [
      {
        source: '/:path*',
        headers: [...allPathsHeaders, ...securityHeaders]
      }
    ];

    return headers;
  },
  modularizeImports: {
    lodash: {
      transform: 'lodash/{{member}}'
    }
  },
  // https://github.com/nexdrew/next-build-id
  // Base Next's build ID on Git info rather than a random string
  generateBuildId: () => nextBuildId({ dir: __dirname, describe: true }),
  webpack: (config, { webpack, buildId }) => {
    config.plugins.push(
      new webpack.DefinePlugin({
        // https://docs.sentry.io/platforms/javascript/guides/nextjs/configuration/tree-shaking/
        __SENTRY_DEBUG__: false
        // __SENTRY_TRACING__: false
      }),
      new webpack.DefinePlugin({
        'process.env.NEXT_PUBLIC_BUILD_ID': JSON.stringify(buildId),
        // eslint-disable-next-line n/no-process-env
        'process.env.NEXT_PUBLIC_NODE_ENV': JSON.stringify(process.env.NODE_ENV)
      })
    );

    return config;
  },
  // Remove the x-powered-by header as some pentesters/scanners consider it an
  // information disclosure.
  poweredByHeader: false,
  eslint: {
    ignoreDuringBuilds: true
  },
  transpilePackages: [
    '@sindresorhus/slugify',
    'escape-string-regexp',
    '@sindresorhus/transliterate'
  ],
  sentry: {
    hideSourcemaps: false
  }
};

module.exports = withSentryConfig(bundleAnalyzer(nextConfig), {
  silent: true
});

// module.exports = bundleAnalyzer(nextConfig);

Steps to Reproduce

I'm sorry, I don't have a minimum viable reproduction for this, and it's a private repo. I can try and put something together next week perhaps, but I wanted to raise this issue in case one of the maintainers recognised the issue.

My environment:

❯ npx next info

    Operating System:
      Platform: darwin
      Arch: x64
      Version: Darwin Kernel Version 22.5.0: Thu Jun  8 22:22:20 PDT 2023; root:xnu-8796.121.3~7/RELEASE_ARM64_T6000
    Binaries:
      Node: 18.14.0
      npm: 9.6.4
      Yarn: N/A
      pnpm: 7.29.0
    Relevant Packages:
      next: 13.4.10
      eslint-config-next: 13.4.10
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.1.6
    Next.js Config:
      output: N/A

Expected Result

Site can be viewed in dev mode

Actual Result

Site errors when a page is viewed in dev mode when withSentryConfig is used, but works when withSentryConfig is not used.

- error TypeError: Cannot destructure property 'RawSource' of 'require(...)' as it is undefined.
    at @sentry/nextjs (/Users/wolsph/Projects/shop-next/.next/server/app/(main)/page.js:22:18)
    at __webpack_require__ (/Users/wolsph/Projects/shop-next/.next/server/webpack-runtime.js:33:43)
    at eval (./sentry.server.config.js:2:72)
    at (sc_server)/./sentry.server.config.js (/Users/wolsph/Projects/shop-next/.next/server/app/(main)/page.js:4643:1)
    at __webpack_require__ (/Users/wolsph/Projects/shop-next/.next/server/webpack-runtime.js:33:43)
    at eval (./src/app/layout.tsx:6:82)
    at (sc_server)/./src/app/layout.tsx (/Users/wolsph/Projects/shop-next/.next/server/app/(main)/page.js:1548:1)
    at Function.__webpack_require__ (/Users/wolsph/Projects/shop-next/.next/server/webpack-runtime.js:33:43)
- error TypeError: Cannot destructure property 'RawSource' of 'require(...)' as it is undefined.
    at @sentry/nextjs (/Users/wolsph/Projects/shop-next/.next/server/app/(main)/page.js:22:18)
    at __webpack_require__ (/Users/wolsph/Projects/shop-next/.next/server/webpack-runtime.js:33:43)
    at eval (./sentry.server.config.js:2:72)
    at (sc_server)/./sentry.server.config.js (/Users/wolsph/Projects/shop-next/.next/server/app/(main)/page.js:4643:1)
    at __webpack_require__ (/Users/wolsph/Projects/shop-next/.next/server/webpack-runtime.js:33:43)
    at eval (./src/app/layout.tsx:6:82)
    at (sc_server)/./src/app/layout.tsx (/Users/wolsph/Projects/shop-next/.next/server/app/(main)/page.js:1548:1)
    at Function.__webpack_require__ (/Users/wolsph/Projects/shop-next/.next/server/webpack-runtime.js:33:43)
digest: "1055278946"
- error Error [TypeError]: Cannot destructure property 'RawSource' of 'require(...)' as it is undefined.
    at Object.<anonymous> (/Users/wolsph/Projects/shop-next/node_modules/@sentry/webpack-plugin/src/index.js:5:9)
    at Module._compile (node:internal/modules/cjs/loader:1226:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1280:10)
    at Module.load (node:internal/modules/cjs/loader:1089:32)
    at Module._load (node:internal/modules/cjs/loader:930:12)
    at Module.require (node:internal/modules/cjs/loader:1113:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (/Users/wolsph/Projects/shop-next/node_modules/@sentry/webpack-plugin/src/cjs.js:1:18)
    at Module._compile (node:internal/modules/cjs/loader:1226:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1280:10)
    at Module.load (node:internal/modules/cjs/loader:1089:32)
    at Module._load (node:internal/modules/cjs/loader:930:12)
    at Module.require (node:internal/modules/cjs/loader:1113:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (/Users/wolsph/Projects/shop-next/node_modules/@sentry/nextjs/cjs/config/webpack.js:10:29)
    at Module._compile (node:internal/modules/cjs/loader:1226:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1280:10)
    at Module.load (node:internal/modules/cjs/loader:1089:32)
    at Module._load (node:internal/modules/cjs/loader:930:12)
    at Module.require (node:internal/modules/cjs/loader:1113:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (/Users/wolsph/Projects/shop-next/node_modules/@sentry/nextjs/cjs/config/withSentryConfig.js:7:17)
    at Module._compile (node:internal/modules/cjs/loader:1226:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1280:10)
    at Module.load (node:internal/modules/cjs/loader:1089:32)
    at Module._load (node:internal/modules/cjs/loader:930:12)
    at Module.require (node:internal/modules/cjs/loader:1113:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (/Users/wolsph/Projects/shop-next/node_modules/@sentry/nextjs/cjs/index.server.js:4:26)
    at Module._compile (node:internal/modules/cjs/loader:1226:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1280:10)
    at Module.load (node:internal/modules/cjs/loader:1089:32)
    at Module._load (node:internal/modules/cjs/loader:930:12)
    at Module.require (node:internal/modules/cjs/loader:1113:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at @sentry/nextjs (/Users/wolsph/Projects/shop-next/.next/server/app/(main)/page.js:22:18)
    at __webpack_require__ (/Users/wolsph/Projects/shop-next/.next/server/webpack-runtime.js:33:43)
    at eval (webpack-internal:///(sc_server)/./sentry.server.config.js:2:72)
    at (sc_server)/./sentry.server.config.js (/Users/wolsph/Projects/shop-next/.next/server/app/(main)/page.js:4643:1)
    at __webpack_require__ (/Users/wolsph/Projects/shop-next/.next/server/webpack-runtime.js:33:43)
    at eval (webpack-internal:///(sc_server)/./src/app/layout.tsx:6:82)
    at (sc_server)/./src/app/layout.tsx (/Users/wolsph/Projects/shop-next/.next/server/app/(main)/page.js:1548:1)
    at Function.__webpack_require__ (/Users/wolsph/Projects/shop-next/.next/server/webpack-runtime.js:33:43)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async /Users/wolsph/Projects/shop-next/node_modules/next/dist/server/app-render/app-render.js:1016:91 {
  digest: undefined
}

philwolstenholme avatar Jul 14 '23 16:07 philwolstenholme

Hey @philwolstenholme, thanks for writing in. I think our SDK has some issues with Next 13.4.10, we identified this in https://github.com/getsentry/sentry-javascript/issues/8507 as well as got a report in https://twitter.com/ThisIsSingleton/status/1679879733444755457.

If you downgrade your Next version, do you still get errors?

AbhiPrasad avatar Jul 14 '23 16:07 AbhiPrasad

Does not happen after downgrading, this is only happening on 13.4.10 (somewhere after 13.4.10-canary.2 that I tested with before)

grabbou avatar Jul 14 '23 17:07 grabbou

We're still investigating what the issue here is - but if anyone has any ideas/suggestions they're greatly appreciated!

AbhiPrasad avatar Jul 14 '23 17:07 AbhiPrasad

i have the same problem, it happens in 13.4.10 v

krychaxp avatar Jul 15 '23 00:07 krychaxp

I'm having the same problem with next 13.4.7

jjrise avatar Jul 15 '23 03:07 jjrise

Hey @philwolstenholme, thanks for writing in. I think our SDK has some issues with Next 13.4.10, we identified this in #8507 as well as got a report in https://twitter.com/ThisIsSingleton/status/1679879733444755457.

If you downgrade your Next version, do you still get errors?

I've been experiencing this issue since bumping my libs from Next 13.4.9 to 13.4.10 & sentry/nextjs 7.47.0 to 7.58.1.

~~I just tried downgrading Next back to 13.4.9 and the issue is still occurring.~~

edit: oops, ignore my previous message. I'd forgotten to remove the caret in "next": "^13.4.9", so my project was still installing 13.4.10 🤦.

The issue is gone after pinning versions and using Sentry 7.58.1 with Next 13.4.9, so something in Next 13.4.10 is breaking Sentry

jkhaui avatar Jul 15 '23 04:07 jkhaui

Hey @philwolstenholme, thanks for writing in. I think our SDK has some issues with Next 13.4.10, we identified this in #8507 as well as got a report in https://twitter.com/ThisIsSingleton/status/1679879733444755457. If you downgrade your Next version, do you still get errors?

I've been experiencing this issue since bumping my libs from Next 13.4.9 to 13.4.10 & sentry/nextjs 7.47.0 to 7.58.1.

~I just tried downgrading Next back to 13.4.9 and the issue is still occurring.~

edit: oops, ignore my previous message. I'd forgotten to remove the caret in "next": "^13.4.9", so my project was still installing 13.4.10 facepalm.

The issue is gone after pinning versions and using Sentry 7.58.1 with Next 13.4.9, so something in Next 13.4.10 is breaking Sentry

this throws the error:

Error: Cannot find module 'webpack'

by the way, all these is after production has been built and started

godfrednanaowusu avatar Jul 15 '23 08:07 godfrednanaowusu

I can also confirm downgrading Next 13.4.10 to 13.4.9 solved this issue for me... I'm on Sentry 7.58.1

jjrise avatar Jul 15 '23 17:07 jjrise

I had the same issue and downgraded Next.js to 13.4.9 for now

jinsley8 avatar Jul 15 '23 23:07 jinsley8

can confirm

thai-recurmetrics avatar Jul 16 '23 18:07 thai-recurmetrics

Hello The issue also happens on If you have sentry 7.58.0.

If you upgrade to sentry 7.58.1 and Next.js 13.4.9, the error issue disappears

georgebastion avatar Jul 17 '23 06:07 georgebastion

Hi all, quick update: We still believe the issue is not related to a specific, recent, Sentry SDK version but just to the latest Next 13.4.10 version. Going back to 13.4.9 fixes this issue regardless of the Sentry SDK version.

We have a somewhat ugly hotfix (#8543) to work around this but I'm not too happy with it, given that we don't know the root cause. That being said, if anyone has an idea what specific change in NextJS might cause the import of webpack-sources to suddenly fail during dev mode, please let us know :)

Lms24 avatar Jul 17 '23 09:07 Lms24

Does that mean Next prod doesn't have this issue? Haven't tested yet myself.

On Mon, 17 Jul 2023, 11:05 Lukas Stracke, @.***> wrote:

Hi all, quick update: We still believe the issue is not related to a specific, recent, Sentry SDK version but just to the latest Next 13.4.10 version. Going back to 13.4.9 fixes this issue regardless of the Sentry SDK version.

We have a somewhat ugly hotfix (#8543 https://github.com/getsentry/sentry-javascript/pull/8543) to work around this but I'm not too happy with it, given that we don't know the root cause. That being said, if anyone has an idea what specific change in NextJS might cause the import of @sentry/webpack-plugin to suddenly fail during dev mode, please let us know :)

— Reply to this email directly, view it on GitHub https://github.com/getsentry/sentry-javascript/issues/8541#issuecomment-1637648761, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAE7SAKQF3UA24GDAWH6C43XQT55RANCNFSM6AAAAAA2KQ4ZDM . You are receiving this because you are subscribed to this thread.Message ID: @.***>

shehi avatar Jul 17 '23 09:07 shehi

Does that mean Next prod doesn't have this issue?

You mean npm run build? No this seems to work fine.

Just noticed that this issue was also raised in the NextJS repo: https://github.com/vercel/next.js/issues/52620

Lms24 avatar Jul 17 '23 09:07 Lms24

I'm having the same problem with next 13.4.7

with this nextjs version, i have noticed the sentry version that works with it is

@sentry/nextjs =7.57.0

godfrednanaowusu avatar Jul 17 '23 09:07 godfrednanaowusu

To everyone following this thread: We just released a beta version with the aforementioned hotfix.

If you can, please help us out and update to @sentry/[email protected] and [email protected]. Please let us know if this fixes things for you again. Thanks!

Lms24 avatar Jul 17 '23 11:07 Lms24

For me, this also happened with next build && next start(thus not only with next dev).

sentry/[email protected] with [email protected] fixed the problem for me (for both next dev and next build && next start).

jl75 avatar Jul 17 '23 12:07 jl75

@Lms24 this seems to be working. Also, development environment also improved dramatically as well. It used to take 10-20 seconds now it's 1-2 seconds to load.

thai-recurmetrics avatar Jul 17 '23 12:07 thai-recurmetrics

To everyone following this thread: We just released a beta version with the aforementioned hotfix.

If you can, please help us out and update to @sentry/[email protected] and [email protected]. Please let us know if this fixes things for you again. Thanks!

This works perfectly well, both in dev mode and production mode

I also notice my project is faster.

godfrednanaowusu avatar Jul 17 '23 13:07 godfrednanaowusu

@sentry/[email protected] works as expected for us.

alvarlagerlof avatar Jul 17 '23 13:07 alvarlagerlof