techie_questie
techie_questie

Reputation: 1522

How to set new port while serving application through static-server in React JS

I have two applications which I want to run on two different ports. I am using webpack to bundle all my static contents in a dist folder and running the static-server. static-server default runs on 9080 and the first application runs perfectly fine. However, when I try to run the second application, I get an error that port is already in use. So I am setting a new PORT for another application so that it serves on that port. To do so, I am doing something like below. In my package.json-

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "webpack-dev-server --hot  --config webpack.prod.js",
    "build": "webpack --config webpack.prod.js",
    "start:prod": "set PORT=3006 && cd dist && static-server"
  }

I am not able to set the port to 3006 and instead when I try to run status-server inside list folder, I get an error-

* Shutting down server

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

Error: listen EADDRINUSE :::9080
    at Object._errnoException (util.js:1022:11)
    at _exceptionWithHostPort (util.js:1044:20)
    at Server.setupListenHandle [as _listen2] (net.js:1351:14)
    at listenInCluster (net.js:1392:12)
    at Server.listen (net.js:1476:7)
    at StaticServer.start (/usr/local/lib/node_modules/static-server/server.js:114:58)
    at Object.<anonymous> (/usr/local/lib/node_modules/static-server/bin/static-server.js:48:8)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)

I also tried to set port in my webpack.config.js inside deserver but still getting the same issue. Can someone please let me know what I am doing wrong.

Upvotes: 0

Views: 2872

Answers (1)

Masious
Masious

Reputation: 430

You should give the port to your process, as an argument, not as an environment variable, like:

static-server -p 8080

Some notes on setting environment variables

If you're in a UNIX-based Operating system link macOS or Linux, there are multiple ways to set environment variables:

  1. If you want the variable to be only set for the latter command, you should use it this way

    PORT=8080 npm run dev.

  2. If you want to set the variable for the entire terminal session, you should set it this way

    $ set -a $ PORT=8080 & yarn run dev

  3. You can add it to your ~/.zshrc / ~/.bashrc ~/.profile to have it always in your terminal:

    export PORT=8080

Upvotes: 1

Related Questions