Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Sentry/Next.js] Not working with Webpack 5 on Next.js 11 #3688

Closed
BjoernRave opened this issue Jun 16, 2021 · 14 comments · Fixed by #3786
Closed

[Sentry/Next.js] Not working with Webpack 5 on Next.js 11 #3688

BjoernRave opened this issue Jun 16, 2021 · 14 comments · Fixed by #3786
Labels
Package: nextjs Issues related to the Sentry Nextjs SDK Type: Bug

Comments

@BjoernRave
Copy link

When trying to start my app with next 11, where webpack 5 is enabled by default I get the following error:

[Sentry] Could not initialize SDK. Received error:
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined

Packages:
"@sentry/nextjs": "^6.7.1",
"next": "^11.0.0",

@AbhiPrasad
Copy link
Member

Hey thank you for reporting. Could you show us a copy of your next.config.js? That would help us start debugging this.

@AbhiPrasad AbhiPrasad added Package: nextjs Issues related to the Sentry Nextjs SDK Status: Needs Information and removed Status: Needs Triage labels Jun 16, 2021
@BjoernRave
Copy link
Author

BjoernRave commented Jun 16, 2021

sure:

// This file sets a custom webpack configuration to use your Next.js app
// with Sentry.
// https://nextjs.org/docs/api-reference/next.config.js/introduction
// https://docs.sentry.io/platforms/javascript/guides/nextjs/

const { withSentryConfig } = require('@sentry/nextjs')
const withI18n = require('next-translate')

const moduleExports = withI18n({
  async redirects() {
    return [
      {
        source: '/tr',
        destination: '/testresult',
        permanent: true,
      },
      {
        source: '/c',
        destination: '/consent',
        permanent: true,
      },
    ]
  },
})

const SentryWebpackPluginOptions = {
  silent: true,
  // Additional config options for the Sentry Webpack plugin. Keep in mind that
  // the following options are set automatically, and overriding them is not
  // recommended:
  //   release, url, org, project, authToken, configFile, stripPrefix,
  //   urlPrefix, include, ignore
  // For all available options, see:
  // https://github.com/getsentry/sentry-webpack-plugin#options.
}

// Make sure adding Sentry options is the last code to run before exporting, to
// ensure that your source maps include changes from all other Webpack plugins
module.exports = withSentryConfig(moduleExports, SentryWebpackPluginOptions)

@MatheusBueno
Copy link

@BjoernRave the sdk is not getting the sentry.server.config.js file
The projet use a env to this file -> SENTRY_SERVER_INIT_PATH.
So a hotfix, i put a

SENTRY_SERVER_INIT_PATH="sentry.server.config.js"

in my .env

@tm1000
Copy link

tm1000 commented Jun 16, 2021

I actually had an error similar to this yesterday but it was caused by a cascading of errors from usage of next-images which the functionality is now supported out of the box by default in NextJS 11. I just went through and cleared out usage of next-images and then upgraded back to nextjs 11, first deleting the .next cache folder and Sentry works there without issue in NextJS 11

See: twopluszero/next-images#73

@jamesopti
Copy link

jamesopti commented Jun 17, 2021

UPDATE: This was caused by omitting the include directory in my SentryWebpackPlugin, which was necessary to match the custom output directory of our next files.

UPDATE: I've been seeing this before Next 11, so likely unrelated.

I too am seeing this error after upgrading to next 11:

➜  client git:(main) yarn info next @sentry/nextjs
├─ @sentry/nextjs@npm:6.4.1
│  ├─ Instances: 1
│  ├─ Version: 6.4.1
└─ next@npm:11.0.0
   ├─ Instances: 1
   ├─ Version: 11.0.0
17:43:50.200 | info  - Creating an optimized production build...
-- | --
17:44:45.352 | DEBUG   2021-06-17 00:44:45.352453210 +00:00 sentry-cli version: 1.64.2, platform: "linux", architecture: "x86_64"
17:44:45.353 | INFO    2021-06-17 00:44:45.352570245 +00:00 sentry-cli was invoked with the following command line: "/vercel/path0/node_modules/@sentry/cli/sentry-cli" "releases" "files" "9ec7e65e3fdb7ff3cc267e68fc4efd558e989c2a" "upload-sourcemaps" ".next/" "--ignore" "node_modules" "--ignore" "webpack.config.js" "--rewrite" "--strip-prefix" "webpack://_N_E/" "--url-prefix" "~/_next"
17:44:45.355 | error: .next/: IO error for operation on .next/: No such file or directory (os error 2)
17:44:45.356 | DEBUG   2021-06-17 00:44:45.356040706 +00:00 client close; no transport to shut down  (from sentry)
17:44:45.356 | DEBUG   2021-06-17 00:44:45.356069663 +00:00 skipping update nagger because session is not attended
17:44:57.050 | DEBUG   2021-06-17 00:44:57.050514121 +00:00 sentry-cli version: 1.64.2, platform: "linux", architecture: "x86_64"
17:44:57.051 | INFO    2021-06-17 00:44:57.050590606 +00:00 sentry-cli was invoked with the following command line: "/vercel/path0/node_modules/@sentry/cli/sentry-cli" "releases" "files" "9ec7e65e3fdb7ff3cc267e68fc4efd558e989c2a" "upload-sourcemaps" ".next/" "--ignore" "node_modules" "--ignore" "webpack.config.js" "--rewrite" "--strip-prefix" "webpack://_N_E/" "--url-prefix" "~/_next"
17:44:57.053 | error: .next/: IO error for operation on .next/: No such file or directory (os error 2)
17:44:57.054 | DEBUG   2021-06-17 00:44:57.054078279 +00:00 client close; no transport to shut down  (from sentry)
17:44:57.054 | DEBUG   2021-06-17 00:44:57.054112270 +00:00 skipping update nagger because session is not attended
17:44:59.605 | info  - Compiled successfully

@joneath
Copy link

joneath commented Jun 17, 2021

I too was running into this issue and was able to work around it by tweaking @MatheusBueno's SENTRY_SERVER_INIT_PATH ENV variable snippet. Here's what worked for me:

SENTRY_SERVER_INIT_PATH=.next/server/sentry/initServerSDK.js

I'm not sure why the default behavior of Sentry is not to use this path but it could very well just be the way I have my app structured, built (docker image), and hosted (Aliyun web services).

@mikestopcontinues
Copy link

I mistakenly posted on #3700 about a closely related issue. I'm receiving this error after updating to Next 11:

[Sentry] Could not initialize SDK. Received error:
Error: Cannot find module '/Users/Code/hub/.next/server/sentry/initServerSDK.js'
Require stack:
- /Users/Code/hub/node_modules/@sentry/nextjs/dist/utils/instrumentServer.js
- /Users/Code/hub/node_modules/@sentry/nextjs/dist/index.server.js
- /Users/Code/hub/next.config.js
- /Users/Code/hub/node_modules/next/dist/next-server/server/config.js
- /Users/Code/hub/node_modules/next/dist/server/next.js
- /Users/Code/hub/node_modules/next/dist/server/lib/start-server.js
- /Users/Code/hub/node_modules/next/dist/cli/next-dev.js
- /Users/Code/hub/node_modules/next/dist/bin/next

@joneath's solution would not work in my case, as even after deleting the cache and hard-upgrading everything, .next/server/sentry/initServerSDK.js is NOT generated. In fact, no sentry file is generated anywhere I can find.

@tm1000
Copy link

tm1000 commented Jun 21, 2021

There seems to be an issue with withSentryConfig in NextJS 11 in that it itself returns a single function. Yet what nextjs wants is an object with a function under the webpack property:

withSentryConfig:[Function: finalNextConfig]

Normal settings: {}

To fix this for me I just did this:

~~snip~~

Notice I treat withSentryConfig like a function that it is. This returns {webpack: Function, productionBrowserSourceMaps: true}

Realized my mistake in #3722

@hangwoo
Copy link

hangwoo commented Jun 22, 2021

I too was running into this issue and was able to work around it by tweaking @MatheusBueno's SENTRY_SERVER_INIT_PATH ENV variable snippet. Here's what worked for me:

SENTRY_SERVER_INIT_PATH=.next/server/sentry/initServerSDK.js

I'm not sure why the default behavior of Sentry is not to use this path but it could very well just be the way I have my app structured, built (docker image), and hosted (Aliyun web services).

It works for me. Thanks~

@mikestopcontinues
Copy link

@tm1000 If withSentryConfig is the last in your plugins pipeline, it works fine. Next also receives a function in that shape. I'm not sure if sentry is using the phase config that would get passed to it, but they might.

@tm1000
Copy link

tm1000 commented Jun 23, 2021

@mikestopcontinues I should have deleted that. I realized in #3722 that I was not following the directions!

@vaniyokk
Copy link

vaniyokk commented Jul 7, 2021

I have the same issue with webpack 5 and Next 11 after I upgraded node:alpine Docker image from 12 to 16. After reverting to v12, error is gone. Hope it helps in investigation

@mikestopcontinues
Copy link

@vaniyokk Interesting. I'm getting the issue on Node 14. Hopefully that helps a bit.

@lobsterkatie
Copy link
Member

Hi, all.

We've changed our approach to injecting the SDK user's sentry config, and I believe that should render the above problems moot. I'm going to close this, but please do let us know if upgrading to the latest version (6.9.0 as of this writing, but likely to be 6.10.0 by EOD tomorrow) doesn't fix it.

Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Package: nextjs Issues related to the Sentry Nextjs SDK Type: Bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants