How npm start runs a server on port 8000
node.jsAngularjsnode.js Problem Overview
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?
node.js Solutions
Solution 1 - node.js
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
Solution 2 - node.js
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.
Solution 3 - node.js
To change the port
npm start --port 8000
Solution 4 - node.js
To start the port correctly in your desired port use:
npm start -- --port 8000
Solution 5 - node.js
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
Solution 6 - node.js
change "start": "react-scripts start",
to "start": "set PORT=3001 && react-scripts start",
Solution 7 - node.js
Or simply in the terminal
set port=5500 && npm start
Solution 8 - node.js
npm run dev -- --port 3001
npm run on another port in cmd
Solution 9 - node.js
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 "
Solution 10 - node.js
npm start --port 8000
does not work. You should write command like this.
ng serve --port 8000
Solution 11 - node.js
$npm run start -- -p 8000
8000 is port number you can change your port number
Solution 12 - node.js
npm start -- --port "port number"