Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/rozenite-port-in-use.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-native-bundle-discovery-rozenite-plugin": patch
---

Don't crash Metro when port 8071 is already in use. With two Metro servers running at once (for example one per git worktree), the second one used to exit on start with `EADDRINUSE`. It now prints a warning and keeps running without its own panel server.
2 changes: 1 addition & 1 deletion packages/rozenite-plugin/src/modules.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
declare module "@discoveryjs/cli" {
interface DiscoveryServer {
listen(port: number, callback?: () => void): void;
listen(port: number, callback?: () => void): import("node:http").Server;
}
export function createServer(
options: Record<string, unknown>,
Expand Down
21 changes: 20 additions & 1 deletion packages/rozenite-plugin/src/react-native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ try {

let outputJsonPath = defaultOutputJsonPath;

const SERVER_PORT = 8071; // Also hard-coded in public/index.html

function injectBundleDiscovery<T extends MetroConfigLike>(
config: T,
options?: BundleDiscoveryPluginOptions,
Expand Down Expand Up @@ -72,7 +74,24 @@ function runServer(): void {
dev: false,
config: configFile,
configFile,
}).then((server) => server.listen(8071));
})
.then((server) => {
// Several Metro servers can run at once (for example one per git worktree), but the panel
// server always uses the same port. Without this handler, the second Metro crashes on start
// with EADDRINUSE. Now it keeps running without its own panel server.
server.listen(SERVER_PORT).on("error", (error: NodeJS.ErrnoException) => {
console.warn(
error.code === "EADDRINUSE"
? `[react-native-bundle-discovery] Port ${SERVER_PORT} is already in use, probably by another Metro. The Bundle Discovery panel will show that server's bundle, not this one.`
: `[react-native-bundle-discovery] Could not start the panel server on port ${SERVER_PORT}: ${error.message}`,
);
});
})
.catch((error: unknown) => {
console.warn(
`[react-native-bundle-discovery] Could not start the panel server: ${String(error)}`,
);
});
}

let isServerRunning = false;
Expand Down
Loading