SSR fails on Netlify with Sentry + NextJS + Yarn PnP

Server side rendering fails when using Sentry + NextJS + Yarn PnP.

There’s a minimal reproduction at GitHub - zachwe/sentry-netlify-repro, which is deployed on Netlify as https://mellifluous-buttercream-fd4a31.netlify.app.

Note that https://mellifluous-buttercream-fd4a31.netlify.app/test results in an internal server error, while the same app works as expected on Vercel (https://sentry-netlify-repro-5lif66kxj-zachwe.vercel.app/test).

The ___netlify-handler function indicates that there is a dependency issue:

May 15, 12:33:23 PM: cb445d0b ERROR  Error: Cannot find module '@sentry/nextjs'
Require stack:
- /var/task/.next/server/pages/_app.js
- /var/task/node_modules/next/dist/server/require.js
- /var/task/node_modules/next/dist/server/next-server.js
- /var/task/.netlify/functions-internal/___netlify-handler/handlerUtils.js
- /var/task/.netlify/functions-internal/___netlify-handler/___netlify-handler.js
- /var/task/___netlify-handler.js
- /var/runtime/UserFunction.js
- /var/runtime/index.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)
    at Function.Module._load (internal/modules/cjs/loader.js:746:27)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:101:18)
    at Object.8097 (/var/task/.next/server/pages/_app.js:49:18)
    at __webpack_require__ (/var/task/.next/server/webpack-runtime.js:25:42)
    at Object.9514 (/var/task/.next/server/chunks/843.js:9:72)
    at __webpack_require__ (/var/task/.next/server/webpack-runtime.js:25:42)
    at __webpack_exec__ (/var/task/.next/server/pages/_app.js:234:39)
    at /var/task/.next/server/pages/_app.js:235:124 {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/var/task/.next/server/pages/_app.js',
    '/var/task/node_modules/next/dist/server/require.js',
    '/var/task/node_modules/next/dist/server/next-server.js',
    '/var/task/.netlify/functions-internal/___netlify-handler/handlerUtils.js',
    '/var/task/.netlify/functions-internal/___netlify-handler/___netlify-handler.js',
    '/var/task/___netlify-handler.js',
    '/var/runtime/UserFunction.js',
    '/var/runtime/index.js'
  ]
}

I have also filed an issue with Sentry, since Sentry appears to be the only library in our stack that triggers this issue: Server side rendering fails on Netlify with Sentry + NextJS + Yarn PnP · Issue #5107 · getsentry/sentry-javascript · GitHub.

But I also wanted to file it here since this issue occurs on Netlify and not on Vercel.

Hey @zachwe,

Could you try to add the following to your netlify.toml to see if it’s working:

[functions."___netlify-handler"]
  external_node_modules = ["@sentry/nextjs"]