I have a create-react-app bootstrapped project i want to deploy on an instance. Right now there are two ways that I have tried to set up the deployment which only ends up setting the port and not the host.
My project architecture is as below
Project
- Frontend
- Backend
My main issue is that although I am able to set the port on create-react-app build I cannot set the hostname to 0.0.0.0 instead of localhost.
In create-react app i hardcoded HOST=0.0.0.0
and PORT=443
in the start script.
Other thing i tried is have a script the serves the pages on port 443. Build serves on port 443 but not on 0.0.0.0.
Create-react-app scripts
"scripts": {
"start": "HOST=0.0.0.0 PORT=443 react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
Basic script at the root of the project
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"client": "cd frontend && npm start",
"client-prod": "cd frontend/build && npm install -g serve && serve -s build -l 443",
"server": "cd backend && npm start",
"sass": "node-sass -w frontend/source/styles/ -o frontend/source/styles/ --recursive",
"dev": "concurrently \"npm run server\" \"npm run client\" ",
"prod": "concurrently \"npm run server\" \"npm run client-prod\" "
},
The development on localhost works fine but i need to able to serve my build pages on 0.0.0.0:443 instead of localhost
To change the default port for a create-react-app project, update the start command in your package. json file to specify the port, e.g. "PORT=3456 react-scripts start" on macOS and Linux and "set PORT=3456 && react-scripts start" on Windows.
When we create a new react app using the npx create-react-app command, the default port for the app is 3000. We can access the app from the localhost:3000.
Finally, we can run our built React project locally with the help of the npm package serve . This is helpful to detect any errors we might have with the final version of our project before pushing live to the web. Like create-react-app, we can use npx to run serve without installing it globally on our computer.
For your React app, you'll have to drag and drop the build folder onto the Netlify Dashboard. Run npm run build beforehand to deploy the latest build. You can also connect GitHub, GitLab, or Bitbucket, depending on where your project is stored. This allows automatic deployment whenever you push your changes.
env file instead in the root of your project and set HOST=0.0.0.0 and if you also want to disable the automatic browser opening when you type yarn or npm start, you can also set BROWSER=none. Check this link for more info https://create-react-app.dev/docs/advanced-configuration/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With