Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verify node version when start RN packager #2099

Merged
merged 4 commits into from Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/common/nodeHelper.ts
@@ -0,0 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.

import { ChildProcess } from "./node/childProcess";

export async function getNodeVersion() {
return await new ChildProcess().execToString("node -v");
}
19 changes: 19 additions & 0 deletions src/common/packager.ts
Expand Up @@ -26,6 +26,7 @@ import { findFileInFolderHierarchy } from "./extensionHelper";
import { FileSystem } from "./node/fileSystem";
import { PromiseUtil } from "./node/promise";
import { CONTEXT_VARIABLES_NAMES } from "./contextVariablesNames";
import { getNodeVersion } from "./nodeHelper";

nls.config({
messageFormat: nls.MessageFormat.bundle,
Expand Down Expand Up @@ -57,6 +58,8 @@ export class Packager {
old: "opn-main.js",
};
private static RN_VERSION_WITH_OPEN_PKG = "0.60.0";
private static NODE_AVAIABLE = "18.0.0";
private static RN_VERSION_WITH_PACKER_ISSUE = "0.73.0";
private static JS_INJECTOR_DIRPATH =
findFileInFolderHierarchy(__dirname, "js-patched") || __dirname;
private static NODE_MODULES_FODLER_NAME = "node_modules";
Expand Down Expand Up @@ -128,6 +131,16 @@ export class Packager {
return this.projectPath;
}

private async stopWithlowNode() {
const versionInfo = await ProjectVersionHelper.getReactNativeVersions(this.projectPath);
const isNodeSupported = semver.gte(await getNodeVersion(), Packager.NODE_AVAIABLE);
const isRNWithPackerIssue = semver.gte(
versionInfo.reactNativeVersion,
Packager.RN_VERSION_WITH_PACKER_ISSUE,
);
return isRNWithPackerIssue && !isNodeSupported;
}

public async getPackagerArgs(
projectRoot: string,
rnVersion: string,
Expand Down Expand Up @@ -256,6 +269,12 @@ export class Packager {
packagerSpawnResult.outcome.catch(() => {}); // We ignore all outcome errors
}

if (await this.stopWithlowNode()) {
await this.stop();
throw new Error(
`React Native needs Node.js >= 18. You're currently on version ${await getNodeVersion()}. Please upgrade Node.js to a supported version and try again.`,
);
}
await this.awaitStart();
if (executedStartPackagerCmd) {
this.logger.info(localize("PackagerStarted", "Packager started."));
Expand Down