Search code examples
terminalcreate-react-appgit-bash

Let the git bash choose the next available port automatically (Windows)


I'm developing in React and my package.json has the following code:

"scripts": {
    "start": "craco start",
    "build": "craco build",
    "test": "craco test",
    "eject": "craco eject"
},

I was using Git Bash as a VS Code's embedded terminal. When I'm running multiple projects, I simply open multiple VS Code windows with their own terminal and simply run npm start and they work. If any project is running on Port 3000 the bash automatically suggests/choose the next available port for me.

But recently I'm seeing some performance issues using the embedded terminal from VS Code.

Hence I decided to use standalone Git Bash to run individual projects in watch mode. I opened up the first project and run, then I opened up another Git Bash on another project, but the npm start command simply is exiting with

Something is already running on port 3000.

I've tried many solutions like npm start --port=3001 and npm start -- --port=3001 from my Git Bash, but nothing worked.

How can I let my Git Bash on Windows behave as they behaved in VS Code's embedded terminal?


Solution

  • You may find many solutions here:
    https://stackoverflow.com/a/41770848/1743124

    But one of the out-of-the-box solutions that worked for me in Windows, is... ...using Windows PowerShell

    Windows PowerShell to choose the next available port

    Open Windows PowerShell on the next projects and write npm start The terminal will inform you that the port is been used, and with a prompt, it can run on the next available port.

    PS: On Windows 11, you can have an "Open PowerShell window here" context menu item. 😊