Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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

Open
achou11 opened this issue Feb 27, 2025 · 5 comments · May be fixed by #69
Open

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

achou11 opened this issue Feb 27, 2025 · 5 comments · May be fixed by #69
Assignees

Comments

@achou11
Copy link

achou11 commented Feb 27, 2025

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 😅 :

So with `7000` we should be safe.

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.

@ralphtheninja ralphtheninja self-assigned this Feb 28, 2025
@ralphtheninja
Copy link
Member

ralphtheninja commented Feb 28, 2025

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

@ralphtheninja
Copy link
Member

$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
Copy link
Member

ralphtheninja commented Feb 28, 2025

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 ralphtheninja linked a pull request Feb 28, 2025 that will close this issue
@achou11
Copy link
Author

achou11 commented Feb 28, 2025

@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.).

@ralphtheninja
Copy link
Member

@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

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants