Skip to content

Commit

Permalink
Verify node version when start RN packager (#2099)
Browse files Browse the repository at this point in the history
* Improve launch app process - verify node version when start rn packager

* Improve launch app process - verify node version when start rn packager
  • Loading branch information
lexie011 authored Jan 30, 2024
1 parent 1db42be commit 9fbf922
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/common/nodeHelper.ts
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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

0 comments on commit 9fbf922

Please sign in to comment.