Angular’s development server defaults to port 4200. While this works perfectly well, you may need to change it for various reasons – perhaps another application is already using that port, or you need to serve multiple Angular applications simultaneously on different ports. This tutorial explains the different ways to configure the development server port in your Angular project.
Understanding the Approaches
There are several methods to change the Angular development server port:
- Command-Line Argument: The quickest way to change the port is to use the
--portflag when running theng servecommand. angular.jsonConfiguration: You can permanently set the port by modifying your project’sangular.jsonfile. This is the recommended approach for consistent behavior.package.jsonScript: Adjust theng servecommand within yourpackage.jsonfile’sscriptssection to include the--portflag.- Legacy
angular-cli.json(Angular CLI 1): Older Angular projects using Angular CLI version 1 used a file namedangular-cli.json. Configuration for this is explained below.
1. Using the Command-Line Argument
This is the simplest method for a one-time port change.
Open your terminal, navigate to your Angular project directory, and run:
ng serve --port 5000
Replace 5000 with your desired port number. This will start the development server on the specified port. You can then access your application at http://localhost:5000/.
2. Configuring the Port in angular.json
To make the port change permanent, modify your angular.json file.
- Open
angular.jsonin your project’s root directory. - Locate the
projectssection. Within this section, find your project’s name (e.g., "my-project"). - Inside your project’s configuration, find the
architectsection, and then theserveconfiguration. - Add or modify the
optionssection and set theportproperty:
{
"projects": {
"my-project": {
"architect": {
"serve": {
"options": {
"port": 5000
}
}
}
}
}
}
Save the angular.json file. The next time you run ng serve, it will automatically use port 5000.
Multiple Configurations:
If you have multiple configurations (e.g., "development", "production"), you can specify the port for each:
{
"projects": {
"my-project": {
"architect": {
"serve": {
"configurations": {
"development": {
"port": 5000
},
"production": {
// Other production settings
}
}
}
}
}
}
}
To use a specific configuration, run ng serve --configuration development (or production).
3. Modifying package.json
You can also set the port within your package.json file. This is useful if you want to easily start the server with a specific port using npm start or yarn start.
- Open
package.jsonin your project’s root directory. - Locate the
scriptssection. - Modify the
startscript to include the--portflag:
{
"scripts": {
"start": "ng serve --port 5000"
}
}
Now, when you run npm start or yarn start, the development server will start on port 5000.
4. Configuring angular-cli.json (Legacy – Angular CLI 1)
If you’re working on an older Angular project using Angular CLI version 1, you’ll find the port configuration in the angular-cli.json file.
- Open
angular-cli.jsonin your project’s root directory. - Locate the
defaultssection. - Within the
serveconfiguration, set theportproperty:
{
"defaults": {
"serve": {
"port": 5000
}
}
}
Save the file, and the next time you run ng serve, it will use the specified port.
Best Practices
- Prioritize
angular.json: Modifyingangular.jsonis the recommended approach for consistently setting the port for your project. - Avoid Direct File Modification of CLI files: Never directly modify files within the
node_modulesdirectory. These files are managed by npm/yarn and can be overwritten during updates. - Consider Port Conflicts: Before changing the port, ensure that no other application is already using the desired port on your system.