sentry-javascript: @sentry/nextjs not working on server

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).

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 15 (2 by maintainers)

Most upvoted comments

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

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

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.

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!

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