0

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.

6
  • If your in a unix-based environment (mac or linux), you need to set the port like this: PORT=3006 static-server ./dist Commented Nov 14, 2018 at 14:44
  • I do cd dist and then run static-server. yes, I am trying in Mac, but its not running in my localhost. Commented Nov 14, 2018 at 15:14
  • It still gives me the same port error. Commented Nov 14, 2018 at 15:17
  • 1
    try this one: static-server -p 3306 Commented Nov 14, 2018 at 15:26
  • if I give this on cli, it works but I want this to be in my package.json. when I am adding this in my package.json, it gives the same previous error. Commented Nov 14, 2018 at 15:34

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.