{"record":{"id":"53ef8141534ba90a","repo":"vercel/next.js","slug":"could-not-determine-the-next-codemod-version","errorCode":null,"errorMessage":"Could not determine the @next/codemod version.","messagePattern":"Could not determine the @next/codemod version\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/next/src/cli/next-upgrade.ts","lineNumber":246,"sourceCode":"      const guidePath = join(\n        runDirectory,\n        'docs/01-app/02-guides/upgrading/agentic-upgrade.md'\n      )\n      const guidesSpinner = createSpinner('Preparing upgrade')\n\n      try {\n        for (const router of ['01-app', '02-pages']) {\n          await cp(\n            join(bundledDocs, router, '02-guides/upgrading'),\n            join(runDirectory, 'docs', router, '02-guides/upgrading'),\n            { recursive: true }\n          )\n        }\n\n        if (needsVersionMigration) {\n          const codemodVersion = process.env.__NEXT_VERSION\n          if (!codemodVersion) {\n            throw new Error('Could not determine the @next/codemod version.')\n          }\n          const codemodCommand = `${getNpxCommand(baseDir)} @next/codemod@${codemodVersion} upgrade ${result.targetVersion} --yes --skip-adoption${options.verbose ? ' --verbose' : ''}`\n          const guide = await readFile(guidePath, 'utf8')\n          if (!guide.includes(CODEMOD_COMMAND_PLACEHOLDER)) {\n            throw new Error('Could not prepare the upgrade guide.')\n          }\n          await writeFile(\n            guidePath,\n            guide.replace(CODEMOD_COMMAND_PLACEHOLDER, codemodCommand)\n          )\n        }\n      } catch (error) {\n        await rm(runDirectory, { recursive: true, force: true })\n        throw error\n      } finally {\n        guidesSpinner?.stop()\n      }\n","sourceCodeStart":228,"sourceCodeEnd":264,"githubUrl":"https://github.com/vercel/next.js/blob/34433fd12ee8074ea3f47af9f36255c7390d0301/packages/next/src/cli/next-upgrade.ts#L228-L264","documentation":"When the upgrade requires a version migration, the CLI builds an `npx @next/codemod@<version> upgrade ...` command using the build-time __NEXT_VERSION environment constant. If that env var is not set, the CLI cannot pin a codemod version and throws. This indicates a non-standard or corrupted Next.js build/distribution.","triggerScenarios":"Running a next build where __NEXT_VERSION was not injected (custom/self-compiled bundles, bundler DCE stripping define-plugin replacement, or a repackaged next distribution missing the define).","commonSituations":"Using a locally patched or unofficially built next package; running from a source checkout without the taskfile define-env step; environment stripping of __NEXT_* variables.","solutions":["Reinstall the official next package (npm/pnpm install with the published dist)","Rebuild next with the standard build (pnpm --filter=next build) so define-env injects __NEXT_VERSION","If running from source, run the full bootstrap build instead of executing unbundled CLI code","Verify with `node -e \"console.log(require('next/package.json').version)\"` that the install is intact"],"exampleFix":"// before: executing src/cli/next-upgrade.ts directly via ts-node\n// after: use the built CLI\nnpx next upgrade --ai","handlingStrategy":"try-catch","validationCode":"if (!process.env.__NEXT_VERSION) {\n  console.error('__NEXT_VERSION missing; use an official next build')\n}","typeGuard":null,"tryCatchPattern":"try {\n  await spawnNextUpgrade(dir, options)\n} catch (e) {\n  if (e.message.includes('codemod version')) {\n    console.error('Reinstall the official next package: npm i next@latest')\n  }\n}","preventionTips":["Use published next builds, not hand-rolled bundles","When building next from source, run pnpm --filter=next build so define-env injects __NEXT_VERSION","Do not strip __NEXT_* define replacements in custom bundler configs"],"tags":["nextjs","upgrade","env-var","build"],"backgroundTag":"missing-env-var","analyzedSha":"34433fd12ee8074ea3f47af9f36255c7390d0301","analyzedAt":"2026-09-20T18:20:20.576Z","contentChangedAt":"2026-09-20T18:20:20.576Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}