Create-react-app: Option to specify port when running the server?

Created on 22 Nov 2016  Â·  15Comments  Â·  Source: facebook/create-react-app

My project is based on create-react-app. npm start by default will run the application on port 3000 and there is no option of specifying a port in the package.json.

How can I specify a port of my choice in this case? I want to run two of this project simultaneously (for testing), one in port 3005 and other is 3006

SO Question

Most helpful comment

You can also create an .env file with

PORT=3006

and save it in your project directory.

All 15 comments

You can specify your port with process.env.PORT. It can be done by changing the script for start in package.json as shown below:

"start": "PORT=3006 react-scripts start"

You mean my package.json will be

{
  "start": "PORT=3006 react-scripts start",
  "name": "igene-web",
  "version": "0.1.0"......... }

then npm start?

didn't work for me (windows and ubuntu)

@fireflieslive no it is not. You should change the start command inside scripts section in your package.json

haha, where am i looking. thanks!

You're welcome. If your issue is solved you can close the issue :D

works with ubuntu, not with windows:

'PORT' is not recognized as an internal or external command,
operable program or batch file.

The above command is Unix (Ubuntu, Mac, etc…). In windows enviroment, you need a different syntax. Google something like "set env in windows command line"

For windows you can use cross-env https://www.npmjs.com/package/cross-env

You can also create an .env file with

PORT=3006

and save it in your project directory.

This worked for me on elementaryOS, thanks!

On Windows with yarn this worked for me:

"scripts": {
    "start": "set PORT=3006 && react-scripts start",
    ....

Or use npm cross-env (without &&!)

"scripts": {
    "start": "cross-env PORT=3006 react-scripts start",
}

how to do it with the build folder ?

Deployment is a bit different, you can read our documentation on that @webmobiles.
Open an issue if you run into any trouble / find something wrong!

"start": "PORT=3006 react-scripts start"
in package.json change it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AlexeyRyashencev picture AlexeyRyashencev  Â·  3Comments

barcher picture barcher  Â·  3Comments

alleroux picture alleroux  Â·  3Comments

xgqfrms-GitHub picture xgqfrms-GitHub  Â·  3Comments

fson picture fson  Â·  3Comments