question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Docusaurus chooses port 3000 even if it is busy

See original GitHub issue

Have you read the Contributing Guidelines on issues?

Prerequisites

  • I’m using the latest version of Docusaurus.
  • I have tried the npm run clear or yarn clear command.
  • I have tried rm -rf node_modules yarn.lock package-lock.json and re-installing packages.
  • I have tried creating a repro with https://new.docusaurus.io.
  • I have read the console error message carefully (if applicable).

Description

On macOS, consider the following script server.js:

const http = require("http");

const port = 3000;

const requestListener = function (req, res) {
    res.writeHead(200);
    res.end("My first server!");
};

const server = http.createServer(requestListener);

server.listen(port,  () => {
    console.log(`Server is running on http://localhost:${port}`);
});

Run it using node server.js. Then start a Docusaurus project (running on v2.1.0 in my case). The documentation server will start on port 3000, although it is busy. If you then open Docusaurus in Chrome:

image it opens my server.

however, if I use curl, I get the docusaurus page!

Screenshot 2022-10-11 at 11 52 58

If my server passes a host:

- server.listen(port,  () => {
+ server.listen(port, "localhost",  () => {

then Docusaurus will correctly identify that the port is busy and asks if I want to choose another port. Nonetheless I see it as a bug in Docusaurus or one of it’s dependencies.

Possible solution

Although used by less people, this logic for finding a free port works now in Remotion without bug reports coming in: https://github.com/remotion-dev/remotion/blob/main/packages/renderer/src/get-port.ts

I am happy to chat on Discord how to investigate it together!

Reproducible demo

No response

Steps to reproduce

As above

Expected behavior

Busy port detection kicks in and asks if a different port should be used

Actual behavior

Server gets started and a conflict is created

Your environment

  • Docusaurus version used: v2.1.0
  • Environment name and version (e.g. Chrome 89, Node.js 16.4): Node 16.7
  • Operating system and version (e.g. Ubuntu 20.04.2 LTS): macOS

Self-service

  • I’d be willing to fix this bug myself.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
JonnyBurgercommented, Oct 12, 2022

Dug deper:

  • Docusaurus uses detect-port@1.3.2 while Facebook uses detect-port-alt@1.1.6

detect-port-alt uses 4 steps to determine the port (see their comments): https://unpkg.com/detect-port-alt@1.1.6/lib/detect-port.js

detect-port uses only 3steps to determine the port, and the logic is significantly different: https://unpkg.com/detect-port@1.3.0/lib/detect-port.js

  • Facebooks passes 0.0.0.0 as hostname parameter to detect-port-alt, while Docusaurus uses 127.0.0.1 as hostname parameter

I found that if Docusaurus switches to detect-port-alt, and uses 0.0.0.0 instead, it also detects the busy port like CRA. Only tested on macOS though.

1reaction
slorbercommented, Oct 12, 2022

Thanks 💯

If only someone could just create a shared package that works for everyone 😅 looks like CRA is using detect-port-alt now (published by Dan Abramov, no fork repo linked 😓 ). That probably was for a good reason.

Interesting related CRA issues:

Read more comments on GitHub >

github_iconTop Results From Across the Web

keep getting "? Something is already running on port 3000 ...
Something is already running on port 3000" message in my terminal when I start up my react server when there is absolutely nothing...
Read more >
CLI - Docusaurus
When forwarding port 3000 from a remote server or VM (e.g. GitHub Codespaces), you can run the dev server on 0.0.0.0 to make...
Read more >
https://raw.githubusercontent.com/facebook/create-...
Heuristics Normally, `npm start` runs on port `3000`, and this is not ... `npm start` will attempt to run with port `3000` by...
Read more >
Run Your Node API with Express/ TypeScript on Heroku
When I work on customer projects, the cloud providers are usually mandated ... We still need to initialize or configure TypeScript. ... const...
Read more >
Something is already running on port 3000
Hi Everyone I have downloaded a few react project from github for study purpose. When I run the proj... Tagged with ecommerce, react, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found