webxdc-dev icon indicating copy to clipboard operation
webxdc-dev copied to clipboard

Use next available port instead of hardcoded as default when using `run` command

Open achou11 opened this issue 1 year ago • 5 comments

Running webxdc-dev run ... without using the --port flag results in the following error on my machine:

Error: listen EADDRINUSE: address already in use :::7000

Looks like this comment didn't age too well 😅 :

https://github.com/webxdc/webxdc-dev/blob/0b9fafed598f0e8cb7a6d20e40991d40187a5bdd/CHANGELOG.md#L295

Instead of a hardcoded port as the default, I would suggest using a module like https://github.com/sindresorhus/get-port to get the next available port if the preferred one isn't available.

achou11 avatar Feb 27 '25 16:02 achou11

@achou11 Any alternative to get-port available that does the same? This is opening a can of worms because $reasons.

ralphtheninja avatar Feb 28 '25 02:02 ralphtheninja

$reasons

So TypeScript is configured to build commonjs:

Image

Which results in the following built javascript:

Image

I.e. using import in the TypeScript really result in doing the good ole require. Buuuuut get-port doesn't allow you to do that since it's a es6 module. So it would be easier if there was a good ole require module doing the same.

Yes, I have tried changing tsconfig-backend.json as well to produce something else than commonjs but then you run into other issues. It just doesn't stop :)

ralphtheninja avatar Feb 28 '25 02:02 ralphtheninja

Looks like @davidmarkclements has done a version of it here https://github.com/davidmarkclements/get-port/blob/master/index.js

EDIT: Nope, this isn't the code. It does something else and the repository is even gone from github :/

ralphtheninja avatar Feb 28 '25 02:02 ralphtheninja

@ralphtheninja if trying to get a closest match to a preferred port isn't a requirement, pretty sure you can let Express use Node's default behavior of choosing a random available port by specifying 0.

Docs from Express v4 listen() method:

Image

If port is omitted or is 0, the operating system will assign an arbitrary unused port, which is useful for cases like automated tasks (tests, etc.).

achou11 avatar Feb 28 '25 15:02 achou11

@ralphtheninja if trying to get a closest match to a preferred port isn't a requirement, pretty sure you can let Express use Node's default behavior of choosing a random available port by specifying 0.

I found detect-port and it works really well, see #69

ralphtheninja avatar Feb 28 '25 15:02 ralphtheninja