11import * as fs from "fs" ;
2- import * as path from "path" ;
32import { parse as parseSemver } from "semver" ;
43import type { ParsedVersion } from "./types" ;
54
@@ -9,28 +8,15 @@ import type { ParsedVersion } from "./types";
98 * @returns The Next.js version string, or undefined if not found
109 */
1110export function getNextjsVersion ( ) : string | undefined {
12- // Try cwd-based lookup first (works for standard project layouts)
13- try {
14- const nextPackageJsonPath = path . join ( process . cwd ( ) , "node_modules" , "next" , "package.json" ) ;
15-
16- if ( fs . existsSync ( nextPackageJsonPath ) ) {
17- const packageJson = JSON . parse ( fs . readFileSync ( nextPackageJsonPath , "utf-8" ) ) ;
18- return packageJson . version ;
19- }
20- } catch {
21- // Fall through to require.resolve
22- }
23-
24- // Fallback: use Node's module resolution, which handles hoisted packages in monorepos
2511 try {
12+ // Resolve from cwd so we find the app's next, not the SDK's devDependency.
13+ // This also handles monorepos where next is hoisted to the workspace root.
2614 const resolvedPath = require . resolve ( "next/package.json" , { paths : [ process . cwd ( ) ] } ) ;
2715 const packageJson = JSON . parse ( fs . readFileSync ( resolvedPath , "utf-8" ) ) ;
2816 return packageJson . version ;
2917 } catch {
30- // next is not resolvable
18+ return undefined ;
3119 }
32-
33- return undefined ;
3420}
3521
3622/**
0 commit comments