Skip to content

Module not found: Can't resolve '@sentry/utils' with Next.js (app router), pnpm #14316

Closed
@bernardoforcillo

Description

@bernardoforcillo

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/nextjs

SDK Version

8.38.0

Framework Version

React 18.3.1 Next.js 14.2.16

Link to Sentry event

No response

Reproduction Example/SDK Setup

// This file configures the initialization of Sentry on the client.
// The config you add here will be used whenever a users loads a page in their browser.
// https://docs.sentry.io/platforms/javascript/guides/nextjs/

import * as Sentry from '@sentry/nextjs';

Sentry.init({
  dsn: 'https://48f482fd599c8c9c69660053eccd765f@o4508054239838208.ingest.de.sentry.io/4508161681129552',

  enabled: process.env.NODE_ENV === 'production',

  // Add optional integrations for additional features
  integrations: [Sentry.replayIntegration()],

  // Define how likely traces are sampled. Adjust this value in production, or use tracesSampler for greater control.
  tracesSampleRate: 1,

  // Define how likely Replay events are sampled.
  // This sets the sample rate to be 10%. You may want this to be 100% while
  // in development and sample at a lower rate in production
  replaysSessionSampleRate: 0.1,

  // Define how likely Replay events are sampled when an error occurs.
  replaysOnErrorSampleRate: 1.0,

  // Setting this option to true will print useful information to the console while you're setting up Sentry.
  debug: false,
});

// This file configures the initialization of Sentry for edge features (middleware, edge routes, and so on).
// The config you add here will be used whenever one of the edge features is loaded.
// Note that this config is unrelated to the Vercel Edge Runtime and is also required when running locally.
// https://docs.sentry.io/platforms/javascript/guides/nextjs/

import * as Sentry from '@sentry/nextjs';

Sentry.init({
  dsn: 'https://48f482fd599c8c9c69660053eccd765f@o4508054239838208.ingest.de.sentry.io/4508161681129552',

  enabled: process.env.NODE_ENV === 'production',

  // Define how likely traces are sampled. Adjust this value in production, or use tracesSampler for greater control.
  tracesSampleRate: 1,

  // Setting this option to true will print useful information to the console while you're setting up Sentry.
  debug: false,
});

// This file configures the initialization of Sentry on the server.
// The config you add here will be used whenever the server handles a request.
// https://docs.sentry.io/platforms/javascript/guides/nextjs/

import * as Sentry from '@sentry/nextjs';

Sentry.init({
  dsn: 'https://48f482fd599c8c9c69660053eccd765f@o4508054239838208.ingest.de.sentry.io/4508161681129552',

  enabled: process.env.NODE_ENV === 'production',

  // Define how likely traces are sampled. Adjust this value in production, or use tracesSampler for greater control.
  tracesSampleRate: 1,

  // Setting this option to true will print useful information to the console while you're setting up Sentry.
  debug: false,
});

/// Nextjs config:

import { withSentryConfig } from '@sentry/nextjs';
import createNextIntlPlugin from 'next-intl/plugin';
import withBundleAnalyzer from '@next/bundle-analyzer';

const withNextIntl = createNextIntlPlugin('./src/scripts/i18n/request.ts');

const withAnalyzer = withBundleAnalyzer({
  enabled: process.env.NEXT_ANALYZE === 'true',
});

/** @type {import('next').NextConfig} */
const nextConfig = {
  output: 'standalone',
  reactStrictMode: true,
  compiler: {
    reactRemoveProperties: true,
  },
  images: {
    remotePatterns: [
      {
        protocol: 'https',
        hostname: '*.googleusercontent.com',
        port: '',
        pathname: '/a/**',
      },
    ],
  },
};

const withSentry = withSentryConfig(withNextIntl(nextConfig), {
  // For all available options, see:
  // https://github.com/getsentry/sentry-webpack-plugin#options

  org: 'easywhey',
  project: 'platform-nextjs',

  // Only print logs for uploading source maps in CI
  silent: !process.env.CI,

  // For all available options, see:
  // https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/

  // Upload a larger set of source maps for prettier stack traces (increases build time)
  widenClientFileUpload: true,

  // Automatically annotate React components to show their full name in breadcrumbs and session replay
  reactComponentAnnotation: {
    enabled: true,
  },

  // Route browser requests to Sentry through a Next.js rewrite to circumvent ad-blockers.
  // This can increase your server load as well as your hosting bill.
  // Note: Check that the configured route will not match with your Next.js middleware, otherwise reporting of client-
  // side errors will fail.
  tunnelRoute: '/monitoring',

  // Hides source maps from generated client bundles
  hideSourceMaps: true,

  // Automatically tree-shake Sentry logger statements to reduce bundle size
  disableLogger: true,

  // Enables automatic instrumentation of Vercel Cron Monitors. (Does not yet work with App Router route handlers.)
  // See the following for more information:
  // https://docs.sentry.io/product/crons/
  // https://vercel.com/docs/cron-jobs
  automaticVercelMonitors: true,
});

export default withAnalyzer(withSentry);

Steps to Reproduce

Build via pnpm next build works, via podman does not

FROM node:20-alpine AS base
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable
RUN pnpm install -g turbo
FROM base AS deps
RUN apk update
RUN apk add --no-cache libc6-compat
WORKDIR /app
RUN pnpm install -g turbo
COPY . . 
RUN turbo prune @easywhey/platform --docker
FROM base AS builder
RUN apk update
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY --from=deps /app/out/json/ .
RUN pnpm install --frozen-lockfile
ENV NEXT_TELEMETRY_DISABLED 1
ENV NODE_ENV production
COPY --from=deps /app/out/full/ .
RUN pnpm turbo build --filter=@easywhey/platform...
FROM base AS runner
WORKDIR /app
ENV NODE_ENV production
ENV NEXT_TELEMETRY_DISABLED 1
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
COPY --from=builder --chown=nextjs:nodejs /app/apps/platform/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/apps/platform/.next/static ./apps/platform/.next/static
COPY --from=builder --chown=nextjs:nodejs /app/apps/platform/public ./apps/platform/public
USER nextjs
EXPOSE 3000
CMD ["node", "apps/app/server.js"]

Expected Result

Build the container

Actual Result

@easywhey/platform:build: cache miss, executing 487442c12d267ce5
@easywhey/platform:build: 
@easywhey/platform:build: > @easywhey/platform@ build /app/apps/platform
@easywhey/platform:build: > next build
@easywhey/platform:build: 
@easywhey/platform:build:   ▲ Next.js 14.2.16
@easywhey/platform:build:   - Environments: .env
@easywhey/platform:build:   - Experiments (use with caution):
@easywhey/platform:build:     · instrumentationHook
@easywhey/platform:build: 
@easywhey/platform:build:    Creating an optimized production build ...
@easywhey/platform:build: Failed to compile.
@easywhey/platform:build: 
@easywhey/platform:build: ./src/app/api/*
@easywhey/platform:build: Module not found: Can't resolve '@sentry/utils'
@easywhey/platform:build: 
@easywhey/platform:build: https://nextjs.org/docs/messages/module-not-found
@easywhey/platform:build: 
@easywhey/platform:build: ./src/app/api/*
@easywhey/platform:build: Module not found: Can't resolve '@sentry/utils'
@easywhey/platform:build: 
@easywhey/platform:build: https://nextjs.org/docs/messages/module-not-found
@easywhey/platform:build: 
@easywhey/platform:build: ./src/app/api/*
@easywhey/platform:build: Module not found: Can't resolve '@sentry/utils'
@easywhey/platform:build: 
@easywhey/platform:build: https://nextjs.org/docs/messages/module-not-found
@easywhey/platform:build: 
@easywhey/platform:build: ./src/app/api/*
@easywhey/platform:build: Module not found: Can't resolve '@sentry/utils'
@easywhey/platform:build: 
@easywhey/platform:build: https://nextjs.org/docs/messages/module-not-found
@easywhey/platform:build: 
@easywhey/platform:build: ./src/app/api/*
@easywhey/platform:build: Module not found: Can't resolve '@sentry/utils'
@easywhey/platform:build: 
@easywhey/platform:build: https://nextjs.org/docs/messages/module-not-found
@easywhey/platform:build: 
@easywhey/platform:build: 
@easywhey/platform:build: > Build failed because of webpack errors
@easywhey/platform:build:  ELIFECYCLE  Command failed with exit code 1.
@easywhey/platform:build: ERROR: command finished with error: command (/app/apps/platform) /usr/local/bin/pnpm run build exited (1)
@easywhey/platform#build: command (/app/apps/platform) /usr/local/bin/pnpm run build exited (1)

Seem like the bug effect only /api/* routes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Package: nextjsIssues related to the Sentry Nextjs SDK

    Type

    Projects

    Status

    Waiting for: Community

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions