123

I recently used a angular-seed folder from github for angular application development. In some previous angularjs tutorial there was a script folder and a server.js file in the angular-seed folder which had all the configuration for running the node server. So how does npm now just start running a node server and where is all the configuration of that node server?

mattsson
  • 1,320
  • 1
  • 14
  • 30
user3724677
  • 1,321
  • 2
  • 10
  • 10
  • It's in `package.json` file. See: https://github.com/angular/angular-seed/blob/master/package.json – Raghavendra Jul 15 '14 at 05:18
  • 1
    I came here searching for a non-angular solution (Next.js which is basically React) and found it solution in [this answer below](https://stackoverflow.com/a/60451410/5369706). Using Next.js, at least for me, ports aren't defined in the `package.json` file; `npm run dev -- --port ####` let me specify the port. – Jason R Stevens CFA Apr 16 '20 at 12:57

12 Answers12

113

We have a react application and our development machines are both mac and pc. The start command doesn't work for PC so here is how we got around it:

"start": "PORT=3001 react-scripts start",
"start-pc": "set PORT=3001&& react-scripts start",

On my mac:

npm start

On my pc:

 npm run start-pc
Andrii Abramov
  • 8,945
  • 8
  • 66
  • 87
YeeHaw1234
  • 1,990
  • 1
  • 15
  • 15
  • 1
    This really saved my day! I was looking for a correct to start node on port different from 3000 for Express. Thanks a lot!. By the way, start command does work on PC. It looks like this in my config `"start": "set PORT=3001 && node ./bin/www"` – Konstantin Jun 26 '17 at 04:46
  • Yes, I mistyped. Start does work on PC, but the syntax for setting the port is different than Mac/Linux. That's why I have 2 "start" commands. one is "start" for mac, and one is "start-pc" for my pc. – YeeHaw1234 Jun 27 '17 at 13:16
100

If you will look at package.json file.

you will see something like this

 "start": "http-server -a localhost -p 8000"

This tells start a http-server at address of localhost on port 8000

http-server is a node-module.

Update:- Including comment by @Usman, ideally it should be present in your package.json but if it's not present you can include it in scripts section.

Mritunjay
  • 24,184
  • 7
  • 51
  • 67
100

To change the port

npm start --port 8000
MANISH KUMAR CHOUDHARY
  • 3,326
  • 3
  • 21
  • 32
Kotireddy
  • 1,027
  • 1
  • 5
  • 2
80

To start the port correctly in your desired port use:

npm start -- --port 8000

theocikos
  • 934
  • 6
  • 6
65

You can change the port in the console by running the following on Windows:

SET PORT=8000

For Mac, Linux or Windows WSL use the following:

export PORT=8000

The export sets the environment variable for the current shell and all child processes like npm that might use it.

If you want the environment variable to be set just for the npm process, precede the command with the environment variable like this (on Mac and Linux and Windows WSL):

PORT=8000 npm run start
datashaman
  • 6,821
  • 2
  • 20
  • 28
Skylin R
  • 1,754
  • 1
  • 16
  • 23
3

change "start": "react-scripts start", to "start": "set PORT=3001 && react-scripts start",

Dinesh M
  • 39
  • 2
3

Or simply in the terminal

set port=5500 && npm start
Ashish Saini
  • 148
  • 1
  • 11
1
npm run dev -- --port 3001

npm run on another port in cmd

Sehrish Waheed
  • 504
  • 5
  • 9
0

The solution which will work on every machine, let it be MAC, Window, Linux or any other, just specify the port where you want to run. In your package.json do this :

 "start": "export PORT=3001 && react-scripts start "
0

npm start --port 8000 does not work. You should write command like this.

ng serve --port 8000
ibrahim
  • 3
  • 2
0

$npm run start -- -p 8000

8000 is port number you can change your port number

-1
npm start -- --port "port number"