Skip to content

Commit d5efc43

Browse files
authored
Fix next version check (#409)
* fix package version * change variable name * Create giant-pianos-float.md
1 parent c2817fe commit d5efc43

File tree

3 files changed

+20
-23
lines changed

3 files changed

+20
-23
lines changed

Diff for: .changeset/giant-pianos-float.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"open-next": patch
3+
---
4+
5+
Fix next version check

Diff for: packages/open-next/src/build.ts

+4-15
Original file line numberDiff line numberDiff line change
@@ -187,15 +187,15 @@ function setStandaloneBuildMode(monorepoRoot: string) {
187187
}
188188

189189
function buildNextjsApp(packager: "npm" | "yarn" | "pnpm" | "bun") {
190-
const { nextPackageJsonPath } = options;
190+
const { appPackageJsonPath } = options;
191191
const command =
192192
config.buildCommand ??
193193
(["bun", "npm"].includes(packager)
194194
? `${packager} run build`
195195
: `${packager} build`);
196196
cp.execSync(command, {
197197
stdio: "inherit",
198-
cwd: path.dirname(nextPackageJsonPath),
198+
cwd: path.dirname(appPackageJsonPath),
199199
});
200200
}
201201

@@ -213,19 +213,8 @@ function printHeader(header: string) {
213213
}
214214

215215
function printNextjsVersion() {
216-
const { appPath } = options;
217-
cp.spawnSync(
218-
"node",
219-
[
220-
"-e",
221-
`"console.info('Next.js v' + require('next/package.json').version)"`,
222-
],
223-
{
224-
stdio: "inherit",
225-
cwd: appPath,
226-
shell: true,
227-
},
228-
);
216+
const { nextVersion } = options;
217+
logger.info(`Next.js version : ${nextVersion}`);
229218
}
230219

231220
function printOpenNextVersion() {

Diff for: packages/open-next/src/build/helper.ts

+11-8
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,19 @@ export function normalizeOptions(config: OpenNextConfig, root: string) {
2525
);
2626
const outputDir = path.join(buildOutputPath, ".open-next");
2727

28-
let nextPackageJsonPath: string;
28+
let appPackageJsonPath: string;
2929
if (config.packageJsonPath) {
3030
const _pkgPath = path.join(process.cwd(), config.packageJsonPath);
31-
nextPackageJsonPath = _pkgPath.endsWith("package.json")
31+
appPackageJsonPath = _pkgPath.endsWith("package.json")
3232
? _pkgPath
3333
: path.join(_pkgPath, "./package.json");
3434
} else {
35-
nextPackageJsonPath = findNextPackageJsonPath(appPath, root);
35+
appPackageJsonPath = findNextPackageJsonPath(appPath, root);
3636
}
3737
return {
3838
openNextVersion: getOpenNextVersion(),
39-
nextVersion: getNextVersion(nextPackageJsonPath),
40-
nextPackageJsonPath,
39+
nextVersion: getNextVersion(appPath),
40+
appPackageJsonPath,
4141
appPath,
4242
appBuildOutputPath: buildOutputPath,
4343
appPublicPath: path.join(appPath, "public"),
@@ -209,9 +209,12 @@ export function getOpenNextVersion(): string {
209209
return require(path.join(__dirname, "../../package.json")).version;
210210
}
211211

212-
export function getNextVersion(nextPackageJsonPath: string): string {
213-
const version = require(nextPackageJsonPath)?.dependencies?.next;
214-
// require('next/package.json').version
212+
export function getNextVersion(appPath: string): string {
213+
// We cannot just require("next/package.json") because it could be executed in a different directory
214+
const nextPackageJsonPath = require.resolve("next/package.json", {
215+
paths: [appPath],
216+
});
217+
const version = require(nextPackageJsonPath)?.version;
215218

216219
if (!version) {
217220
throw new Error("Failed to find Next version");

0 commit comments

Comments
 (0)