Make npm start script run ng serve in a particular port

Viewed 28707

I have some scripts in my package.json, and I need to know how to get the start script to correctly accept the -port parameter for angular-cli.

  "scripts": {
    "ng": "ng",
    "start": "ng serve  --proxy-config proxy.conf.json",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },

We need this because we would like to run multiple instances of our software simultaneously. Currently, if I have one project running on the default port 4200, and try to run npm start -port 4300 in a second terminal window, I get, "Port 4200 is already in use. Use '--port' to specify a different port."

What can I do to get my build to run in a particular port? And how can I make it so that I can pass the port number into the npm start script from the command line?

3 Answers

If you add "--" the parameters are passed through:

npm start -- --port 4301

Change it as,

"scripts": {
    "ng": "ng",
    "start": "ng serve --port 4301",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },

You can try this command:

npm run start -- -p 4300
Related