{"record":{"id":"ed87d0d58b18022b","repo":"anomalyco/sst","slug":"failed-to-detect-next-js-version-using-opennext-v","errorCode":null,"errorMessage":"Failed to detect Next.js version. Using OpenNext v${DEFAULT_OPEN_NEXT_VERSION} as default.","messagePattern":"Failed to detect Next\\.js version\\. Using OpenNext v(.+?) as default\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"platform/src/components/aws/nextjs.ts","lineNumber":596,"sourceCode":"    super(__pulumiType, name, args, opts);\n  }\n\n  protected normalizeBuildCommand(args: NextjsArgs) {\n    return all([args?.buildCommand, args?.openNextVersion, args?.path]).apply(\n      ([buildCommand, openNextVersion, sitePath]) => {\n        if (buildCommand) return buildCommand;\n\n        function detectDefaultOpenNextVersion() {\n          try {\n            const pkgPath = path.join(sitePath ?? \".\", \"package.json\");\n            const pkg = JSON.parse(fs.readFileSync(pkgPath, \"utf-8\"));\n            const nextVersion =\n              pkg.dependencies?.next ?? pkg.devDependencies?.next;\n            if (nextVersion && isALtB(nextVersion, \"15.0.0\")) {\n              return DEFAULT_OPEN_NEXT_VERSION_NEXT14;\n            }\n          } catch {\n            console.warn(`Failed to detect Next.js version. Using OpenNext v${DEFAULT_OPEN_NEXT_VERSION} as default.`);\n          }\n          return DEFAULT_OPEN_NEXT_VERSION;\n        }\n\n        const version = openNextVersion ?? detectDefaultOpenNextVersion();\n        const packageName = isALteB(version, \"3.1.3\")\n          ? \"open-next\"\n          : \"@opennextjs/aws\";\n        return `npx --yes ${packageName}@${version} build`;\n      },\n    );\n  }\n\n  protected buildPlan(\n    outputPath: Output<string>,\n    name: string,\n    args: NextjsArgs,\n    { bucket }: { bucket: Bucket },","sourceCodeStart":578,"sourceCodeEnd":614,"githubUrl":"https://github.com/anomalyco/sst/blob/a0bd20f762883e72a35caccb4896c42ce5b3f707/platform/src/components/aws/nextjs.ts#L578-L614","documentation":"The Nextjs component inspects package.json to decide whether to use OpenNext v2 (Next.js 14) or the default OpenNext version. When the package.json cannot be read or parsed (or `next` cannot be resolved from dependencies/devDependencies), detectDefaultOpenNextVersion logs this console.warn and falls back to the default OpenNext version rather than failing the deploy.","triggerScenarios":"Calling sst nextjs component construction (openNextVersion not explicitly set) while the app's package.json is missing, unreadable, malformed JSON, lacks a `next` entry in dependencies/devDependencies, or uses a non-semver `next` version string that breaks isALtB comparison inside the try block.","commonSituations":"Deploying a Next.js app whose package.json was not included (wrong `root`/`path` config pointing at a directory without package.json); monorepo paths misconfigured; a `next` version like `workspace:*` or a file: URL that the semver comparator rejects; deploying a Next.js 15+ app (returns default path but the catch still fires on parse issues).","solutions":["Verify the component's `path`/`root` points to the directory containing the Next.js app's package.json","Ensure the app has `next` listed in dependencies (or devDependencies) with a valid semver version (e.g. \"15.1.0\") — replace placeholders like `workspace:*` or `file:` specs","Fix any JSON syntax errors in package.json (`npx jsonlint package.json` or `cat package.json | node -e 'JSON.parse(require(\"fs\").readFileSync(0))'`)","Pin the OpenNext version explicitly: `sst nextjs.Nextjs(..., { openNextVersion: \"3.x.x\" })` to skip auto-detection and silence the warning"],"exampleFix":"// before (sst.config.ts, path misconfigured)\nnew sst.aws.Nextjs(\"Web\", { path: \"packages/webapp\" }) // no package.json there\n// after\nnew sst.aws.Nextjs(\"Web\", { path: \"apps/web\" }) // apps/web/package.json has \"next\": \"15.1.0\"","handlingStrategy":"validation","validationCode":"const pkg = JSON.parse(fs.readFileSync(path.join(appPath, \"package.json\"), \"utf8\"));\nconst next = pkg.dependencies?.next ?? pkg.devDependencies?.next;\nif (!next || !semver.valid(semver.coerce(next))) {\n  throw new Error(`package.json at ${appPath} has no valid semver \"next\" dependency; got: ${next}`);\n}","typeGuard":"function isValidNextVersion(v: unknown): v is string {\n  return typeof v === \"string\" && /^(\\^|~|>=?)?\\d+\\.\\d+\\.\\d+/.test(v) && !v.startsWith(\"workspace:\") && !v.startsWith(\"file:\");\n}","tryCatchPattern":"try {\n  const pkg = JSON.parse(fs.readFileSync(pkgPath, \"utf8\"));\n  // use pkg.dependencies?.next\n} catch (err) {\n  console.warn(`Failed to detect Next.js version (${err}); set openNextVersion explicitly in sst.config.ts`);\n  return DEFAULT_OPEN_NEXT_VERSION;\n}","preventionTips":["Pin `next` to an explicit semver version in package.json — avoid workspace:/file:/latest specs","Set `openNextVersion` explicitly on the Nextjs component to bypass auto-detection","Confirm the component's `path` points at the folder containing package.json","Keep package.json valid JSON and committed to the deploy context (not gitignored)"],"tags":["nextjs","opennext","fallback","version-detection"],"backgroundTag":"version-detection-failed","analyzedSha":"a0bd20f762883e72a35caccb4896c42ce5b3f707","analyzedAt":"2026-08-30T11:26:00.383Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}