Change Default Port in SAP CAP Project
When developing SAP CAP (Cloud Application Programming) projects, you may need to run the application on a custom port other than the default one. This can be achieved by modifying the configuration in your package.json file or by using environment variables. In this blog post, we’ll walk through both methods to help you run your SAP CAP application on a different port.
Method 1: Configuring Port in package.json
The first and easiest method is to set the port directly within the package.json file under the cds section.
- Open your
package.jsonfile. - Add the following configuration to set the server port:
{
"name": "my-cap-project",
"version": "1.0.0",
"dependencies": {
"@sap/cds": "^5"
},
"cds": {
"server": {
"port": 4000
}
}
}
In this example, the CAP application will run on port 4000. You can change 4000 to any port number that suits your environment.
Method 2: Setting the Port Using Environment Variables
Another method is to define the port dynamically using an environment variable. This approach is useful when you want to keep the port configuration flexible across different environments.
On Linux/MacOS, run the following command in the terminal:
PORT=4000 cds run
On Windows, use the set command as shown below:
set PORT=4000 && cds run
This will run the application on port 4000 for that particular session.
Method 3: Using the .env File
If you prefer a more permanent and environment-specific configuration, you can create a .env file in the root of your project to set the port.
- Create a
.envfile in the project root. - Add the following content:
PORT=4000
This method is useful when you want to maintain different configurations for different environments (e.g., development, production).
Conclusion
By using any of the above methods, you can easily configure your SAP CAP application to run on a custom port. This flexibility allows you to manage multiple applications on the same machine or avoid port conflicts during development. Whether you choose to modify package.json, use environment variables, or leverage a .env file, CAP makes it easy to set the port for your application.
Happy coding! 🚀
Feel free to experiment with the port configuration method that best suits your project needs. Let us know in the comments if you have any questions or additional tips!