160

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,329
  • 1
  • 14
  • 30
user3724677
  • 1,691
  • 3
  • 12
  • 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

13 Answers13

139

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
  • 10,019
  • 9
  • 74
  • 96
YeeHaw1234
  • 2,358
  • 1
  • 17
  • 16
  • 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
123

To start the port correctly in your desired port use:

npm start -- --port 8000
Juuso Ohtonen
  • 8,826
  • 9
  • 65
  • 98
theocikos
  • 1,684
  • 1
  • 9
  • 7
113

To change the port

npm start --port 8000
MANISH KUMAR CHOUDHARY
  • 3,396
  • 3
  • 22
  • 34
Kotireddy
  • 1,197
  • 1
  • 6
  • 2
110

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
  • 25,338
  • 7
  • 55
  • 68
  • 1
    I couldn't find this in my package.json, however adding this under the "scripts": {} section worked for me. – Usman Jan 12 '17 at 08:05
  • @Usman Ideally it should be there in your `package.json`, but if not there you can always add it. Updated the answer, thanks for pointing it. – Mritunjay Jan 12 '17 at 08:23
  • 1
    you just start some http server, but not react-scripts. PORT=3001 react-scripts start solves the problem – Ilya Khudyakov Oct 21 '19 at 19:25
  • I think in newer versions of npm it's located in "launch.json". – Karis Sr. Dec 18 '21 at 10:08
78

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
  • 8,301
  • 3
  • 22
  • 29
Skylin R
  • 2,111
  • 1
  • 20
  • 23
8
npm run dev -- --port 3001

npm run on another port in cmd

Sehrish Waheed
  • 1,230
  • 14
  • 17
6

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

Dinesh M
  • 73
  • 1
  • 2
6

Or simply in the terminal

set port=5500 && npm start
Ashish Saini
  • 190
  • 3
  • 11
2
npm start -- --port "port number"
2

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 "
1
npm run start -- -p 8000

8000 is port number you can change your port number

Mechanic
  • 5,015
  • 4
  • 15
  • 38
0

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

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

(For those still suffering despite trying all the answers.)

Here is a quick fix that should not be used in production!

Just forward the source port to the target one that you need. Open another shell and tap this command:

socat tcp-listen:8000,reuseaddr,fork tcp:192.168.1.48:3000

In this example; 3000 is the port of the server and 8000 the port you want to forward to (or receive from to be precise)

Enjoy!

amine2029
  • 36
  • 2