Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Option to specify an environment variable specifically for create-react-app's dev and test mode PORT #13117

Open
ariccio opened this issue Apr 12, 2023 · 0 comments

Comments

@ariccio
Copy link

ariccio commented Apr 12, 2023

Is your proposal related to a problem?

A tiny, but consistently frustrating issue for me and many many devs out there is that the PORT env var is shared for a lot of things. If you have a classic split rails backend and react frontend, you can't reasonably easily start them by script. There are ugly and fragile options. When running things manually, its a minor annoyance, I have to wait to start the backend, then come back and run the react scripts start command to manually tell it "yeah, sure just use damn port 3001, that's fine!".

Most recently, this was a hassle for me when setting up proper end-to-end tests for co2trackers with cypress. I have to hardcode things, and its still fragile as hell.

Describe the solution you'd like

There's a comment in the code about cloud9, so hopefully this doesn't interact poorly.

Add another line of code under the line for selecting DEFAULT_PORT like so:

const DEFAULT_PORT = parseInt(process.env.PORT, 10) || 3000;
const ALTERNATE_REQUESTED_PORT = (process.env?.CRA_SPECIFIC_START_PORT && parseInt(process.env?.CRA_SPECIFIC_START_PORT, 10))

...then later:

    if (ALTERNATE_REQUESTED_PORT !== undefined) {
        console.log(`Found CRA_SPECIFIC_START_PORT: ${ALTERNATE_REQUESTED_PORT}. using...`);
        return choosePort(HOST, ALTERNATE_REQUESTED_PORT);
    }
    // We attempt to use the default port but if it is busy, we offer the user to
    // run on a different port. `choosePort()` Promise resolves to the next free port.
    return choosePort(HOST, DEFAULT_PORT);

Describe alternatives you've considered

Alternatively, cra could use a command line param. Personally, I tend to prefer command line params over env vars because env vars are the ultimate global variable. They tend to lead to problems exactly like this 😆. I do understand the necessity - the comment about cloud9 suggests this is a reasonable design and I'm not suggesting you change it - otherwise tools have to do odd things like interposing processes. I remember back in the days when I tried to get into static analysis, the clang analyzer had to do some ugly things to interpose and build databases of all the commands executed.

Personally, my programming style is a ton more verbose than this codebase. If it were up to me, I'd also add a few lines to check if CRA_SPECIFIC_START_PORT exists, but doesn't seem like an integer, and print a nice warning. I find that going to the extra effort to validate these unlikely cases ends up saving me untold hours down the line when the unlikely inevitably happens.

Yes, I'm the kind of person who checks the return value of printf and fclose. You'd be amazed what you find.

Additional context

I dunno what else to say. This is a small annoyance that affects probably millions of people. Plenty of issues have been filed for it. It's been bugging me for a while and I'm finally getting around to fixing it instead of doing the actual important work I have to do trying to fight the ongoing pandemic 😉🤣. If every programmer in the world fixed at least one of the small little things that bugged them, the whole world would be a better place!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant