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/nextjs not working on server #3691

Closed
5 of 9 tasks
cyrus-za opened this issue Jun 16, 2021 · 15 comments · Fixed by #3786 or #3811
Closed
5 of 9 tasks

@sentry/nextjs not working on server #3691

cyrus-za opened this issue Jun 16, 2021 · 15 comments · Fixed by #3786 or #3811
Labels
Package: nextjs Issues related to the Sentry Nextjs SDK Type: Bug

Comments

@cyrus-za
Copy link

Package + Version

  • @sentry/browser
  • @sentry/node
  • raven-js
  • raven-node (raven for node)
  • other: nextjs

Version:

6.7.1

Description

When I scafold a nextjs app (using nx workspaces, but probably unrelated) and add sentry to it, the frontend works just fine, but running next dev results in pages/api/* routes not logging to sentry. I do use the withSentry wrapper around my handlers, but it appears that Sentry.init never gets called despite sentry.server.config.js being configured. Calling it manually right outside the handler fixes it, so it's definitely got to do with the cli not calling init. I am guessing the init gets called during webpack builds which is configured to only run in production (I dont want to upload source maps for local dev).

@lobsterkatie
Copy link
Member

@cyrus-za Thanks for reporting this. What version of nextjs are you using, and which version of webpack?

Also, can you clarify, are you using next dev simply because you don't want to upload sourcemaps, or for other reasons? If it's the former, you can use dryRun: true in your SentryWebpackPluginOptions to prevent sourcemap upload while you're developing. We enable that conditionally based on whether you're running the dev or production server, but you could just as well set it based on an environment variable.

@ckychris
Copy link

Some more info here:
It will load the 2nd time it runs, suggesting the cache creation timing is different.
I am using 11.0.1-canary.2 since it fixed some blocking issues.

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

@neilpoulin
Copy link

neilpoulin commented Jun 17, 2021

Same issue here. Just tried upgrading to next 11. I've only tried starting my nextjs app in dev mode so far. I get the same log messages as @ckychris, reproduced here:

ready - started server on 0.0.0.0:3000, url: http://localhost:3000
info  - Loaded env from /Users/myproject/.env.local
info  - Loaded env from /Users/myproject/.env
info  - Using webpack 5. Reason: Enabled by default https://nextjs.org/docs/messages/webpack5
not using sentry config wrapper;
[Sentry] Could not initialize SDK. Received error:
Error: Cannot find module '/Users/myproject/.next/server/sentry/initServerSDK.js'
Require stack:
- /Users/myproject/node_modules/@sentry/nextjs/dist/utils/instrumentServer.js
- /Users/myproject/node_modules/@sentry/nextjs/dist/index.server.js
- /Users/myproject/next.config.js
- /Users/myproject/node_modules/next/dist/next-server/server/config.js
- /Users/myproject/node_modules/next/dist/server/next.js
- /Users/myproject/node_modules/next/dist/server/lib/start-server.js
- /Users/myproject/node_modules/next/dist/cli/next-dev.js
- /Users/myproject/node_modules/next/dist/bin/next
event - compiled successfully
event - build page: /dashboard
wait  - compiling...

My app does indeed start, and everything looks normal.

One thing to note: in dev mode, when NODE_ENV !== 'production', i do not include the sentry webpack plugin. My full next.config.js looks like this:

const withBundleAnalyzer = require('@next/bundle-analyzer')({
    enabled: process.env.ANALYZE === 'true',
});
const isProd = process.env.NODE_ENV === 'production';
const { withSentryConfig } = require('@sentry/nextjs');

const SentryWebpackPluginOptions = {
    authToken: process.env.SENTRY_AUTH_TOKEN,
    setCommits: {
        commit: process.env.APP_VERSION,
        repo: process.env.SENTRY_REPO,
    },
};

const nextConfig = {
    target: 'server',
    webpack5: true,
    poweredByHeader: false,
    productionBrowserSourceMaps: true,

    publicRuntimeConfig: {
        apiHost: process.env.API_HOST,
        // ... other env variables
    },
};

const sentryConfig = isProd ? withSentryConfig(nextConfig, SentryWebpackPluginOptions) : nextConfig;
console.log(isProd ? 'using sentry config wrapper' : 'not using sentry config wrapper');
module.exports = withBundleAnalyzer(sentryConfig);

Edit: i also get this same error/warning when creating a production build and running the prod server via npm run build and npm run start. I do not see the error during the build step. It only shows up the first time i try to load a page after building and starting the server.

@juanignaciomolina
Copy link

Same issue as @neilpoulin here after updating to Next 11 and running npm run dev.

@baptisteArno
Copy link

baptisteArno commented Jun 20, 2021

I had the same issue with next 10.2.2. I had to add webpack 5 support:

// 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 moduleExports = {
  // Your existing module.exports
  future: {
    webpack5: true,
  },
};

const SentryWebpackPluginOptions = {
  // 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);

now it catches error from API routes

Edit

But still, it doesn't catch errors on production build... It only works when calling Sentry.init in the function file directly. It seems that init function in sentry.server.config.js is never called in production environment. My Next.js project runs Typescript. Could this be linked?

@mikestopcontinues
Copy link

Same issue after upgrading to Next 11. Linked to these issues:

#3688
#3700

@paslo22
Copy link

paslo22 commented Jun 23, 2021

Same issue here, using @sentry/nextjs 6.7.2 and next 10.2.3:

Error: Cannot find module '/var/www/app/.next/server/sentry/initServerSDK.js'
Require stack:
- /var/www/app/node_modules/@sentry/nextjs/dist/utils/instrumentServer.js
- /var/www/app/node_modules/@sentry/nextjs/dist/index.server.js
- /var/www/app/next.config.js
- /var/www/app/node_modules/next/dist/next-server/server/config.js
- /var/www/app/node_modules/next/dist/server/next.js
- /var/www/app/node_modules/next/dist/server/lib/start-server.js
- /var/www/app/node_modules/next/dist/cli/next-dev.js
- /var/www/app/node_modules/next/dist/bin/next

@Vadorequest
Copy link

The error Error: Cannot find module '/var/www/app/.next/server/sentry/initServerSDK.js' is a false-positive, it was explained here.

@Vadorequest
Copy link

Calling Sentry.flush() is necessary on the API endpoints. I've done some testing with Amplitude (analytics) and without flushing Amplitude, events weren't reported properly. I suspect the same rule goes with Sentry, but maybe it only fails from time to time. (?)

See UnlyEd/next-right-now#375
See UnlyEd/next-right-now#377

Also, I recommend wrapping "flush" into try/catch, as it throws exception under some race conditions. See #3748 and https://github.com/UnlyEd/next-right-now/blob/v2-mst-aptd-at-lcz-sty/src/modules/core/sentry/universal.ts#L56-L63 for implementation example.

@juanjgarcia
Copy link

We're also seeing Error: Cannot find module 'Documents/GitHub/proj/.next/server/sentry/initServerSDK.js' on our builds.

"@sentry/nextjs": "^6.7.1",
"next": "^10.2.3",
"webpack": "^4.42.0"

@mikestopcontinues
Copy link

Just wanted to add, v6.8.0 does not solve this problem.

@natterstefan
Copy link

Hi @baptisteArno,

Edit

But still, it doesn't catch errors on production build... It only works when calling Sentry.init in the function file directly. It seems that init function in sentry.server.config.js is never called in production environment. My Next.js project runs Typescript. Could this be linked?

Which "in the function file directly" do you mean? Can you explain that to me in more detail, please? I have some issues getting @sentry/nextjs up and running on Vercel at the moment. Thanks for your help.

@baptisteArno
Copy link

I mean calling init before your Next.js handler function (any file in api folder)

@natterstefan
Copy link

Ok, but everything else works as expected on Vercel for you?

@lobsterkatie
Copy link
Member

Hi, all.

There were two problems causing this, I believe, one of them fixed here (already released) and one of them fixed here (should be released tomorrow, as 6.10.0).

Once that release is published, can you all please try upgrading, and let us know if you're still having trouble? I'm going to close this in the meantime, but please do reach out if you have further problems.

Thanks!

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