Repository navigation
Description
Summary
withSentryConfig detects the Next.js version by resolving next/package.json from process.cwd(). When the process's cwd is not the Next.js project root, detection returns undefined and the SDK silently falls back to the Next 14 config shape — emitting experimental.instrumentationHook and experimental.serverComponentsExternalPackages, and not emitting serverExternalPackages.
On Next ≥ 15 that means the server-external-packages list is written under a key Next no longer reads, so it is silently not applied. Next then warns about the unrecognized keys, which is the visible symptom but not the important part.
Offending line
build/cjs/config/util.js (identical in the ESM build), present in 10.73.0, 10.74.0 and 10.75.0:
function resolveNextjsPackageJson() {
try {
return createRequire(`${process.cwd()}/`).resolve("next/package.json");
} catch {
return void 0;
}
}The resolution base is the process working directory, not the location of the config module or the Next.js project.
Minimal reproduction
_repro.mjs, placed in a Next.js project root:
import { withSentryConfig } from '@sentry/nextjs/config';
const out = withSentryConfig({}, { silent: true });
const cfg = typeof out === 'function' ? await out() : out;
console.log('cwd :', process.cwd());
console.log('experimental :', JSON.stringify(cfg.experimental ?? null));
console.log('serverExternalPackages :', Array.isArray(cfg.serverExternalPackages) ? cfg.serverExternalPackages.length + ' pkgs' : 'UNSET');A — cwd is the project root:
$ node ./_repro.mjs
cwd : /path/to/frontend
experimental : {"clientTraceMetadata":["baggage","sentry-trace"]}
serverExternalPackages : 22 pkgs
B — identical config, cwd elsewhere (env -u NODE_PATH, since NODE_PATH masks the bug):
$ cd /tmp && env -u NODE_PATH node /path/to/frontend/_repro.mjs
[@sentry/nextjs] The Sentry SDK was not able to determine your Next.js version. …clientTraceMetadata…
[@sentry/nextjs] The Sentry SDK was not able to determine your Next.js version. …instrumentationHook…
cwd : /tmp
experimental : {"instrumentationHook":true,"serverComponentsExternalPackages":["amqplib","connect",…,"tedious"]}
serverExternalPackages : UNSET
Only cwd differs.
Why this is hit in practice
Any setup where the Node process starts somewhere other than the Next.js project root. In our case a single Node process serves an Express API and Next.js: the entry file lives in backend/, the process starts at the repo root, and next is under frontend/node_modules. The build (cd frontend && next build) detects correctly; the runtime re-evaluation of next.config.mjs does not, so build-time and runtime config silently disagree.
Also affects: monorepos with a hoisted or non-root Next app, custom servers, and anything started by a supervisor with its own cwd.
Impact
- Functional (main concern): on Next ≥ 15
serverExternalPackagesis never set, so the auto-instrumented server packages are not externalized. Failure is silent — no error, just a different config. - Cosmetic: Next logs
Unrecognized key(s) in object: 'instrumentationHook', 'serverComponentsExternalPackages' at "experimental". - Misleading turbopack warning:
…compatible with Turbopack on Next.js version 15.4.1 or later. You are currently on undefined.
The documented workaround does not work on Next 16
The log line suggests:
To silence Next.js' warning, explicitly set the
experimental.instrumentationHookoption … toundefined.
On Next 16 that makes it worse — the key is still present on the object, so Next flags it at build time where it previously did not:
⚠ Invalid next.config.mjs options detected:
⚠ Unrecognized key(s) in object: 'instrumentationHook' at "experimental"
Verified against next@16.3.5.
Suggested fix
Resolve relative to something stable rather than process.cwd() — the SDK module's own location (createRequire(import.meta.url) / __filename), with process.cwd() as a fallback. Both resolve correctly in the normal case; only the module-relative one also works when cwd differs.
An escape hatch (an explicit nextjsVersion option on withSentryConfig) would help anyone the heuristic still can't reach.
Versions
@sentry/nextjs |
10.73.0 / 10.74.0 / 10.75.0 (all affected) |
next |
16.3.5 |
| node | 22.18.0 (prod), 26.8.1 (local) |
Metadata
Metadata
Assignees
Projects
- StatusShow more project fieldsWaiting for: Product Owner
Activity
linear-code commentedon Sep 17, 2026
JS-3702
fix(nextjs): Resolve Next.js version relative to the SDK when cwd dif…