My project is based on create-react-app. npm start
or yarn 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
you can find default port configuration at start your app
scroll down and change the port to whatever you want
hope this may help you ;)
If you don't want to set the environment variable, another option is to modify the
scripts
part of package.json from:to
Linux (tested on Ubuntu 14.04/16.04) and MacOS (tested by @aswin-s on MacOS Sierra 10.12.4):
or (may be) more general solution by @IsaacPak
Windows @JacobEnsor solution
Update due to the popularity of my answer: Currently I prefer to use environment variables saved in
.env
file(useful to store sets of variables for differentdeploy
configurations in a convenient and readable form). Don't forget to add*.env
into.gitignore
if you're still storing your secrets in.env
files. Here is the explanation of why using environment variables is better in the most cases. Here is the explanation of why storing secrets in environment is bad idea.To summarize, we have three approaches to accomplish this:
The most portable one will be the last approach. But as mentioned by other poster, add .env into .gitignore in order not to upload the configuration to the public source repository.
More details: this article
For my windows folks I discovered a way to change ReactJS port to run on any port you want.Before running the server go to
In it, search for the line below and change the port number to your desired port
And you are good to go.
This works on both Windows and Linux
package.json
but you propably prefer to create .env with PORT=3006 written inside it
It would be nice to be able to specify a port other than
3000
, either as a command line parameter or an environment variable.Right now, the process is pretty involved:
npm run eject
scripts/start.js
and find/replace3000
with whatever port you want to useconfig/webpack.config.dev.js
and do the samenpm start