diff --git a/.changeset/sweet-dingos-boil.md b/.changeset/sweet-dingos-boil.md new file mode 100644 index 000000000..8183e2afb --- /dev/null +++ b/.changeset/sweet-dingos-boil.md @@ -0,0 +1,6 @@ +--- +"@rnx-kit/metro-service": patch +"@rnx-kit/cli": patch +--- + +Use `node-fetch` only as fallback when current Node version doesn't implement Fetch API diff --git a/packages/cli/src/serve/keyboard.ts b/packages/cli/src/serve/keyboard.ts index 7d4d3c631..28cd0f646 100644 --- a/packages/cli/src/serve/keyboard.ts +++ b/packages/cli/src/serve/keyboard.ts @@ -1,6 +1,6 @@ import { info } from "@rnx-kit/console"; import type { MetroTerminal } from "@rnx-kit/metro-service"; -import fetch from "node-fetch"; +import nodeFetch from "node-fetch"; import qrcode from "qrcode"; import readline from "readline"; import type { DevServerMiddleware } from "./types"; @@ -43,7 +43,10 @@ export function attachKeyHandlers({ break; case "j": { - fetch(devServerUrl + "/open-debugger", { method: "POST" }); + info("Opening debugger..."); + // TODO: Remove `node-fetch` when we drop support for Node 16 + const ftch = "fetch" in globalThis ? fetch : nodeFetch; + ftch(devServerUrl + "/open-debugger", { method: "POST" }); break; } diff --git a/packages/metro-service/src/server.ts b/packages/metro-service/src/server.ts index 4d3fb7440..be4424739 100644 --- a/packages/metro-service/src/server.ts +++ b/packages/metro-service/src/server.ts @@ -1,6 +1,6 @@ import { runServer } from "metro"; import net from "net"; -import fetch from "node-fetch"; +import nodeFetch from "node-fetch"; import { ensureBabelConfig } from "./babel"; type ServerStatus = "not_running" | "already_running" | "in_use" | "unknown"; @@ -48,8 +48,10 @@ export async function isDevServerRunning( return "not_running"; } + // TODO: Remove `node-fetch` when we drop support for Node 16 + const ftch = "fetch" in globalThis ? fetch : nodeFetch; const statusUrl = `${scheme}://${host || "localhost"}:${port}/status`; - const statusResponse = await fetch(statusUrl); + const statusResponse = await ftch(statusUrl); const body = await statusResponse.text(); return body === "packager-status:running" &&