Summoning builders…Compiling dreams…Polishing pixels…Aligning the stars to your roadmap…Brewing the backlog…Forging gold from ideas…Charting the constellations…Waking the render spirits…

    Insights/Cloud & Infrastructure/Running A React Environment, CMD Line For Local Port Hosting Delegation

    Running A React Environment, CMD Line For Local Port Hosting Delegation

    Marco Lopes
    November 05, 2019
    1.6k views
    Cloud & Infrastructure
    CMD
    environment
    localhost
    port
    react
    tutorials
    Running A React Environment, CMD Line For Local Port Hosting Delegation

    A React app is ready to run, you type npm start, and a browser opens on localhost:3000. That feels simple, until another project needs the same port, a teammate wants to test from a phone, or your frontend must talk to a backend running somewhere else.

    These are development-server questions, not React rendering questions. Once you understand how the command line starts a server and assigns its address, managing a local React environment becomes much more predictable.

    This guide reflects a typical Create React App workflow in 2019; tools and interfaces may have changed since then.

    We will set up a React project, run it from Windows Command Prompt, choose its local port, and connect it to a separate backend. Where commands differ, we will also cover PowerShell, macOS, and Linux.

    What Is Actually Running on Localhost?

    React is a JavaScript library for building user interfaces from components. It does not, by itself, open a network port or host a website.

    In a Create React App project, the development tooling starts a server that serves your application to the browser. It also watches source files and rebuilds the application when you save changes.

    Consider this address:

    http://localhost:3000
    

    Its parts have separate jobs:

    • http is the protocol used to request the page.
    • localhost refers to the computer making the request.
    • 3000 identifies the port where the server accepts connections.

    A host binding determines which network interfaces a server listens on. A port distinguishes that server from other services on those interfaces. Two processes generally cannot listen on the same address and port simultaneously.

    “Local port hosting delegation” is not a standard React term. Here, it means deciding which port serves the frontend and, when necessary, forwarding selected requests to another local service.

    1. Prepare Node.js and Your Command Line

    Create React App needs Node.js and npm. For a 2019 environment, install a supported Node.js LTS release compatible with the Create React App version you are using. npm normally comes with Node.js.

    Open a new terminal and check both installations:

    node -v
    npm -v
    

    Both commands should print a version number. If Windows reports that a command is not recognized, close and reopen Command Prompt after installation. If it still fails, check that Node.js is included in your PATH.

    Know which shell you are using

    Windows Command Prompt, PowerShell, and Unix-style shells use different environment-variable syntax. A command that works in a macOS terminal may fail in Windows CMD even when Node.js is installed correctly.

    This guide labels shell-specific examples. Do not copy the visible terminal prompt, such as C:\Projects>, into your command.

    Choose a directory where you can create files without administrator privileges. Running development tools as an administrator is usually unnecessary and can introduce permission problems later.

    2. Create the React Project

    In Windows CMD, navigate to your projects folder. The /d option allows cd to change drives as well as directories:

    cd /d C:\Projects
    npx create-react-app local-react-demo
    cd local-react-demo
    

    Create the parent folder first if it does not exist. The setup process downloads dependencies, so it requires an internet connection and may take a few minutes.

    npx, available with npm 5.2 and later, runs package commands without requiring a permanent global installation. For this workflow, you do not need to run npm install -g create-react-app first.

    Understand the generated files

    The project includes several important locations:

    • src/ contains your React components and application code.
    • public/ contains the HTML template and static assets.
    • package.json describes dependencies and available commands.
    • package-lock.json records dependency resolution for repeatable installs.

    The generated start script looks like this:

    {
      "scripts": {
        "start": "react-scripts start"
      }
    }
    

    This is an excerpt, not a replacement for the entire file. Keep the other generated scripts and settings.

    When you run npm start, npm executes that script using the project's installed react-scripts package.

    3. Start and Verify the Development Server

    From the directory containing package.json, run:

    npm start
    

    Create React App normally starts on port 3000 and opens a browser. If the browser does not open, read the terminal output and visit the reported URL manually.

    Keep the terminal open: it contains the running server process. To confirm that development updates work, edit the visible text in src/App.js, save the file, and check the browser.

    Stop the server with Ctrl+C. Windows CMD may ask you to confirm termination of the batch job.

    A common mistake is running npm start from the parent folder. If npm cannot find package.json, check your location:

    cd
    dir
    

    Then move into the actual project directory before trying again.

    4. Assign a Different Local Port

    Suppose another application already uses port 3000. You can assign this project port 3001 through the PORT environment variable.

    Windows Command Prompt

    Use CMD's set command:

    set "PORT=3001" && npm start
    

    The quoted assignment prevents accidental trailing spaces from becoming part of the value. The && operator runs the next command if the assignment succeeds.

    This variable remains in the current CMD session after the server stops. Clear it when you no longer need it:

    set "PORT="
    

    Windows PowerShell

    PowerShell uses a different syntax:

    $env:PORT = "3001"
    npm start
    

    To remove the setting afterward:

    Remove-Item Env:PORT
    

    macOS and Linux

    In shells such as Bash, use an inline assignment:

    PORT=3001 npm start
    

    This applies the variable to that command rather than permanently changing the shell session.

    Avoid shell-specific package scripts

    This script works in common Unix-style shells but not in ordinary Windows CMD:

    {
      "scripts": {
        "start": "PORT=3001 react-scripts start"
      }
    }
    

    For a project shared across operating systems, prefer the environment-file approach below. It avoids adding shell-dependent syntax to package.json.

    5. Save the Port in an Environment File

    Create a file named .env.development.local in the project root, alongside package.json:

    PORT=3001
    HOST=127.0.0.1
    

    Then restart the server:

    npm start
    

    This configuration chooses port 3001 and limits the server to the local machine through an explicit loopback binding. Visit http://127.0.0.1:3001 to check it.

    Create React App reads environment files at startup. Editing one while the server is running requires a restart; saving a React component is not enough.

    If the server still uses another port, check for a PORT variable already set in your terminal. Existing process environment variables take precedence over values loaded from environment files.

    Separate server settings from application variables

    PORT and HOST configure the development server. Custom variables exposed to browser code use the REACT_APP_ prefix:

    REACT_APP_API_BASE_URL=http://localhost:5000
    

    Application code can read that value through process.env.REACT_APP_API_BASE_URL.

    Never put private keys, passwords, or other secrets into frontend environment variables. Values included in browser code are accessible to users. Keeping an environment file out of version control does not make bundled values secret.

    6. Resolve Port Conflicts Safely

    When the default port is occupied, Create React App may offer another port in an interactive terminal. That is convenient for quick testing, but explicit port assignments are easier to coordinate across multiple projects.

    On Windows, inspect the process listening on port 3000:

    netstat -ano | findstr :3000
    

    Look for a matching local address with the LISTENING state. The final column is its process ID. Inspect that process, replacing 12345 with the actual ID:

    tasklist /FI "PID eq 12345"
    

    If it is an old development server, stop it in its original terminal with Ctrl+C. If that terminal is unavailable and you have verified the process, you can terminate it:

    taskkill /PID 12345
    

    Avoid forcefully killing unidentified processes. Choosing another port is often safer than interrupting a service you do not recognize.

    7. Forward API Requests to a Local Backend

    Changing the frontend port does not connect it to a backend automatically. Suppose your React server runs on 3001 and your API runs on 5000.

    For a simple development arrangement, add a top-level proxy field to the existing package.json:

    "proxy": "http://localhost:5000"
    

    Keep valid JSON syntax, including commas between adjacent properties, and restart the React server. Start the backend separately using its own command.

    Your React code can then make a relative request:

    fetch('/api/status')
      .then(response => {
        if (!response.ok) {
          throw new Error(`Request failed: ${response.status}`);
        }
        return response.json();
      })
      .then(data => console.log(data))
      .catch(error => console.error(error));
    

    For eligible requests not served by the development server, Create React App forwards the request to the proxy target. The backend must actually implement /api/status.

    This keeps the browser request on the frontend's origin and can avoid local cross-origin complications. An absolute request to http://localhost:5000/api/status bypasses this proxy and requires appropriate backend CORS configuration.

    The proxy is development-only. It does not start the backend or configure production routing.

    8. Test From Another Device When Needed

    On a phone, localhost means the phone, not your development computer. For local-network testing, change the host binding:

    HOST=0.0.0.0
    PORT=3001
    

    Restart the server, then find your computer's local IPv4 address:

    ipconfig
    

    If the relevant address is 192.168.1.25, open http://192.168.1.25:3001 on a device connected to the same trusted network.

    0.0.0.0 is a listening address, not the address to enter on the phone. A firewall or wireless client-isolation setting may block access. Allow only the access you need, and do not expose the development server to the public internet through router port forwarding.

    Development Hosting Is Not Deployment

    npm start provides a development environment, not a production hosting strategy. When the application is ready, generate optimized assets:

    npm run build
    

    Deploy the resulting build/ directory with suitable static hosting or a production web server. Production API routing, HTTPS, and fallback handling for client-side routes must be configured separately.

    Takeaway

    React handles the interface; its development server handles local hosting. Start in the correct project directory, use your shell's environment-variable syntax, and keep port assignments explicit. With those basics in place, running multiple applications, forwarding API requests, and testing across devices becomes a controlled workflow rather than trial and error.

    Conversation (0)

    No comments yet. Start the conversation.

    Join the conversation

    Create a free member account to comment.