Reputation: 31
I’m a newbie to web programming.I’ve followed a tutorial to create a simple web application with Angular CLI, then it is said that I can run the app locally using the command npm start
.
It worked and my app was running on localhost:4200
.
What npm start
do to run my app?
I also wonder that what kind of server was being used to host my app, because I did not create any web server (E.g: Nodejs server, etc).
Is there any way to customize this server such as changing the port number?
P.s: I am using Angular 6
Upvotes: 1
Views: 1538
Reputation: 61
When you run your app using npm start
, npm will try to find the configuration file package.json
in your app folder.If it exists, the command specified in the start property
of the scripts object
will be run. In your case, the command is probably ng serve
.
Then, it will use webpack-dev-server
to start a local webserver. In angular.json
, the folder path of the builder used to run the local server is specified in the builder
property of the serve
object.
Go to the builder folder, open the schema.json
. You will see the default port which is 4200
is specified in this file.
The easiest way to change the port is to use ng serve
with the port option. For example, if you want to run your app using port 5000
, use ng serve --port 5000
. You can either run this command directly on cmd or specify it in the start
property of the scripts
object.
Upvotes: 6
Reputation: 6565
When you are running the command npm start, the npm will run the command mentioned in packages.json file
In that file, we will be having various scripts such as start, build, test etc
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
Those scripts will have the command to trigger the angular CLI actions.
Basically npm start is an alias name of these script commands. You can change or create your own set of scripts.
As you already installed the npm, the nodejs gets installed in your machine. The angular CLI will use node server to run your application.
By default the port number is 4200, you can customize the port you wish to run.
Another option is to run ng serve command with the --port option e.g
ng serve --port 5050 (i.e for port 5050)
Alternatively, the command: ng serve --port 0, will auto assign an available port for use.
You can update the script in packages.json based on your needs.
Upvotes: 3