Skip to content

Next.js v12 server side crashing on Vercel #4207

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

Closed
iker-barriocanal opened this issue Dec 1, 2021 · 13 comments · Fixed by #4255
Closed

Next.js v12 server side crashing on Vercel #4207

iker-barriocanal opened this issue Dec 1, 2021 · 13 comments · Fixed by #4255
Labels
Meta: Breaking Package: nextjs Issues related to the Sentry Nextjs SDK

Comments

@iker-barriocanal
Copy link
Contributor

iker-barriocanal commented Dec 1, 2021

Related to #4103.

The Next.js SDK crashes apps on the server-side when deployed on Vercel. It doesn't happen locally.

Error logs
2021-12-01T13:01:48.296Z	31dc2972-71bb-4bcd-8ca1-d0d7502aa36c	ERROR	Error: Cannot find module '/var/task/node_modules/next/dist/server/next.js'. Please verify that the package.json has a valid "main" entry
    at tryPackage (internal/modules/cjs/loader.js:321:19)
    at Function.Module._findPath (internal/modules/cjs/loader.js:534:18)
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:888:27)
    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:93:18)
    at Object.<anonymous> (/var/task/node_modules/@sentry/nextjs/dist/utils/instrumentServer.js:8:14)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32) {
  code: 'MODULE_NOT_FOUND',
  path: '/var/task/node_modules/next/package.json',
  requestPath: 'next'
}
RequestId: 31dc2972-71bb-4bcd-8ca1-d0d7502aa36c Error: Runtime exited with error: exit status 1
Runtime.ExitError

Cause

This error is caused by importing modules from next. In the SDK, this import is required to instrument the server, see

import { default as createNextServer } from 'next';

and the server instrumentation is run when initializing the SDK on the server side, see

// wrap various server methods to enable error monitoring and tracing
instrumentServer();

Solution

On Vercel, the server instrumentation has no effect. Errors and transactions are captured through the custom _error.js page and the withSentry handler. Not instrumenting the server (to not import any modules from next) when an app is deployed on Vercel should fix the issue, and the VERCEL environment variable can be helpful here, as used in

if (process.env.VERCEL) {

Technical details may be more complex, to be defined.

@iker-barriocanal iker-barriocanal added Meta: Breaking Package: nextjs Issues related to the Sentry Nextjs SDK labels Dec 1, 2021
@iker-barriocanal iker-barriocanal added this to the Next.js 12 Support milestone Dec 1, 2021
@rocketman-21
Copy link

Any ETA on this? Been waiting to upgrade to Next12 for quite some time now and have been blocked because of Sentry.

@boennemann
Copy link

boennemann commented Dec 7, 2021

I really don't want to be a dick on an open source issue tracker, but this whole thing is just so damn frustrating. It's entirely blocking nextjs@12, for not just us, but also a ton of users customers it seems.

All the hype around next.js conf – Sentry was a gold sponsor for that – and then it takes two months+ to get the first party integration going, with barely any word, commitment or timeline?

… and there I was thinking I'm tracking errors with Sentry, not causing them …

Even just an official update/timeline would be appreciated.

@smeubank
Copy link
Member

smeubank commented Dec 9, 2021

HI @boennemann and everyone else who is impacted by this.

Totally justified. We have a fix we hope to put out by the end of business week, pending some peer code review on the following branch. We appreciate that Sentry is important to all our users and customers alike. Apologies for the delay. Your message, and others like yours, are much appreciated and taken very seriously by the team here.

https://github.com/getsentry/sentry-javascript/tree/kmclb-nextjs-no-instrumentserver-on-vercel

@boennemann
Copy link

@smeubank Thank you very much. Your answer is much appreciated!

@iker-barriocanal
Copy link
Contributor Author

Hello everyone,

We have made a change that fixes some issues with the Next.js server when deployed on Vercel. These are available in the latest release, v6.16.1. Could you try this new version and provide any feedback?

Thanks!

@swudged
Copy link

swudged commented Dec 11, 2021

Hi @iker-barriocanal 👋

I've tried upgrading to 6.16.1, also removing outputFileTracing: false that I had set as suggested in #4103. I'm still getting the Error: Cannot find module '../browser' error that others were reporting on other threads. This is happening on API routes specifically, everything else seems to be working ok.

@iker-barriocanal
Copy link
Contributor Author

@swudged is this only happening on Vercel? What next.js version are you using? Could you attach the code of your API route, so that we can replicate it? Thanks.

@shinnoki
Copy link

Thank you for fix this issue.

I've tried server side api routes with next: 12.0.4 and @sentry/nextjs: 6.16.1 deployed to Vercel and it worked fine! 🎉
It crashed so far with @sentry/nextjs: 6.15.0.

In our project Automatically expose System Environment Variables is enabled in Vercel settings, but possibly process.env.VERCEL is used internally, so it may crash without enabling that?
https://vercel.com/docs/concepts/projects/environment-variables#system-environment-variables

@iker-barriocanal
Copy link
Contributor Author

@shinnoki, thanks for your message. @swudged, could you confirm whether you have the System Environment Variables exposed? The SDK currently relies on the existence of process.env.VERCEL.

@swudged
Copy link

swudged commented Dec 15, 2021

@iker-barriocanal I will find out and report back, currently on holiday for a few days! Thanks

@curiousercreative
Copy link

@iker-barriocanal I've just been bit by this bug after deploying 12 to production. I was coming here to write that even with the latest packages, this is still a problem, but it's actually working. Perhaps there's some delay between a Vercel deploy being marked as completed and the "functions" deploy? At any rate, appears to be working with the below package versions

excerpt from package.json

"@sentry/integrations": "^6.16.1",
"@sentry/nextjs": "^6.16.1",
"next": "^12.0.7",

@iker-barriocanal
Copy link
Contributor Author

@curiousercreative I'm confused after reading your comment, especially:

I've just been bit by this bug after deploying 12 to production.

[...] this is still a problem, but it's actually working.

At any rate, appears to be working with the below package versions

Are you facing issues when deploying in Vercel with the latest package versions? Is the issue you are facing the same that is described in the issue description? Can you replicate it consistently?

@curiousercreative
Copy link

@iker-barriocanal everything is working, latest packages should be considered a fix. My confusing comment was that I reproduced the issue once after updating packages, likely as a result of client, server, or deploy caches.

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

Successfully merging a pull request may close this issue.

8 participants