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?
-
It's in `package.json` file. See: https://github.com/angular/angular-seed/blob/master/package.json – Raghavendra Jul 15 '14 at 05:18
-
1I 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 Answers
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

- 10,019
- 9
- 74
- 96

- 2,358
- 1
- 17
- 16
-
1This 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
To start the port correctly in your desired port use:
npm start -- --port 8000

- 8,826
- 9
- 65
- 98

- 1,684
- 1
- 9
- 7
-
5This is exactly what I came here searching for. I am using Next.js (React) and can now run the development server on a given port; `npm run dev -- --port 8000` worked like a charm. – Jason R Stevens CFA Apr 16 '20 at 12:52
-
2
-
8
-
7
-
8On the two extra `--` there is a very good answer in this thread @AmirHeshmati https://stackoverflow.com/questions/43046885/what-does-do-when-running-an-npm-command – theocikos Oct 15 '20 at 08:29
-
If this doesn't work for you (as it did not work for me), try the solution suggested below, namely: PORT 8000 npm start – Andrew Einhorn Jul 17 '21 at 16:20
-
To change the port
npm start --port 8000

- 3,396
- 3
- 22
- 34

- 1,197
- 1
- 6
- 2
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.

- 25,338
- 7
- 55
- 68
-
1I 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
-
1you 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
-
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

- 8,301
- 3
- 22
- 29

- 2,111
- 1
- 20
- 23
-
3
-
7I just specify it as part of the start command when I'm already running other projects on 'default' ports. `PORT=
npm start` – Drew Reese Mar 23 '18 at 17:49 -
change "start": "react-scripts start",
to "start": "set PORT=3001 && react-scripts start",

- 73
- 1
- 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 "

- 171
- 2
- 6
npm run start -- -p 8000
8000 is port number you can change your port number

- 5,015
- 4
- 15
- 38

- 85
- 3
npm start --port 8000
does not work. You should write command like this.
ng serve --port 8000

- 3
- 2
(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!

- 36
- 2