Skip to content

Next.js v12 server side crashing on Vercel #4207

Closed
@iker-barriocanal

Description

@iker-barriocanal

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions