3

I need to run my ReactJS application on the port 1234 but when I run yarn dev, I get the following:

$ parcel src/index.html --port 1234

Server running at http://localhost:2493 - configured port 1234 could not be used.

√ Built in 11.45s.

It doesn't tell me why it can't run on port 1234, so I suspected that the port might be in use already. According to this answer, the following should tell me what process is using that port.

Get-Process -Id (Get-NetTCPConnection -LocalPort portNumber).OwningProcess

But that didn't help, it gave the following message:

Get-NetTCPConnection : No MSFT_NetTCPConnection objects found with property 'LocalPort' equal to '1234'. Verify the value of the property and retry.

Which I guess means there is no process bound to port 1234. But if that is the case, why can't I bind to that port?

My package.json is as follows:

{
    "name": "bejebeje.react",
    "version": "1.0.0",
    "description": "bejebeje's react-js frontend",
    "main": "index.js",
    "repository": "git@github.com:JwanKhalaf/Bejebeje.React.git",
    "author": "John",
    "license": "GPL-3.0",
    "dependencies": {
        "@fortawesome/fontawesome-svg-core": "^1.2.19",
        "@fortawesome/free-brands-svg-icons": "^5.9.0",
        "@fortawesome/free-solid-svg-icons": "^5.9.0",
        "@fortawesome/pro-light-svg-icons": "^5.9.0",
        "@fortawesome/pro-regular-svg-icons": "^5.9.0",
        "@fortawesome/pro-solid-svg-icons": "^5.9.0",
        "@fortawesome/react-fontawesome": "^0.1.4",
        "@reach/router": "^1.2.1",
        "oidc-client": "^1.8.2",
        "react": ">=16",
        "react-dom": "^0.14.9 || ^15.3.0 || ^16.0.0-rc || ^16.0",
        "react-icons": "^3.7.0",
        "styled-components": "^4.3.2"
    },
    "scripts": {
        "dev": "parcel src/index.html --port 1234",
        "build": "parcel build src/index.html"
    },
    "devDependencies": {
        "@fortawesome/fontawesome-pro": "^5.9.0",
        "axios": "^0.19.0",
        "parcel-bundler": "^1.12.3",
        "prettier": "^1.16.4",
        "sass": "^1.22.5"
    }
}
J86
  • 11,751
  • 29
  • 115
  • 194

3 Answers3

2

After creating a little C# web server that would attempt to bind to the port 1234 I still couldn't get it to work. It would try to bind, but would throw an exception saying:

An attempt was made to access a socket in a way forbidden by its access permissions.

Anyways, after much pain and research here is what finally worked:

First, disable hyper-v (this will restart your PC, so ensure all work is saved). In PowerShell (as admin) run the following:

dism.exe /Online /Disable-Feature:Microsoft-Hyper-V

When your PC has restarted, you need to reserve the port you want so hyper-v doesn't reserve it back, again via PowerShell as admin, run the following:

netsh int ipv4 add excludedportrange protocol=tcp startport=50051 numberofports=1

Now finally re-enable hyper-V (PC will restart again), again via PowerShell as admin:

dism.exe /Online /Enable-Feature:Microsoft-Hyper-V /All

when your PC has finished and is back up, you should be able to bind to that port successfully.

J86
  • 11,751
  • 29
  • 115
  • 194
0

Testing locally with parcel I was able to get the same error with knowingly forcing the following states:

  • Port in use
  • Unauthorized use of port
  • Invalid port

The error message you get is pretty cruddy. To determine what the actual error is you can try and bind the port using other language or program you wish. Since were in javascript we can use this script

require('http').createServer(function(){}).listen(1234);

In my case I already bound port 1234 in a different application and I receive the following error:

events.js:174
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE: address already in use :::1234
    at Server.setupListenHandle [as _listen2] (net.js:1270:14)
    at listenInCluster (net.js:1318:12)
    at Server.listen (net.js:1405:7)
    at Object.<anonymous> (C:\workdir\ETL_feed\listen.js:1:106)
    at Module._compile (internal/modules/cjs/loader.js:701:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
    at Function.Module._load (internal/modules/cjs/loader.js:531:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
Emitted 'error' event at:
    at emitErrorNT (net.js:1297:8)
    at process._tickCallback (internal/process/next_tick.js:63:19)
    at Function.Module.runMain (internal/modules/cjs/loader.js:757:11)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

As you can see this error message is much more detailed that parcels error. When binding ports many are taken by existing applications for example Wikipedia reports that port 1234 is used by VLC media player (however it reports UDP only).

ug_
  • 10,236
  • 2
  • 32
  • 49
0

I was getting this error on a project, but it turns out that the issue was that my project was running on HTTPS while Parcel.js (v2.0) was bundling over HTTP.

This is my original Parcel.js command:

parcel serve path/to/my.file

And this is the fixed version, which bundles over HTTPS:

parcel serve path/to/my.file --https

I'm sure my solution won't fix every instance of the error, but it's worth considering.

Dharman
  • 21,838
  • 18
  • 57
  • 107
Ray Brown
  • 650
  • 1
  • 8
  • 16