Skip to content

Commit 63693a3

Browse files
fix: simplify Next.js version detection in monorepos (#149)
1 parent f4abd19 commit 63693a3

1 file changed

Lines changed: 3 additions & 17 deletions

File tree

src/nextjs/utils.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as fs from "fs";
2-
import * as path from "path";
32
import { parse as parseSemver } from "semver";
43
import 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
*/
1110
export 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

Comments
 (0)