How to specify a port to run a create- react -app based project?
In React projects created with , you can specify the runtime port by setting the environment variable . Here are several ways to set this environment variable:Using Command Line DirectlyYou can directly set the environment variable in the command line when starting the project. For example, on Unix systems (including macOS and Linux), you can use the following command:On Windows, you can use the command:If you are using Windows PowerShell, the command is different:Using .env Filesupports loading environment variables from a file in the project root directory. You can create a file (if it doesn't exist) and add the following content to specify the port:Every time you run , will load the environment variables from the file.Comprehensive ExampleSuppose your project needs to run on port . First, create a file in your project root directory (or edit it if it already exists) and add the following content:After saving the file, every time you run , the React development server will automatically start on port 3001.If you occasionally need to run on a different port, you can temporarily override the settings in the file from the command line, for example:This way, even if the file specifies port , the application will start on port .Note: The port must be an unused port number. If the specified port is already in use by another application, the React development server will throw an error indicating that the port is occupied.