22
votes

I have been using create react app for a while. 'npm start' or 'yarn start' autoreloads works fine by itself but now I have an another problem. Currently I run the app on express server through the build folder, and I use 'npm run build' since express is serving the built files. There are many api calls which requires the app to be ran through this way. Now it become tedious to manually do 'npm run build' every time. Is there a simple way or work around to build automatically just like 'npm start' without eject the app(I know could eject and configure webpack to do that, but i don't want to go down that path)? Thanks

5
Cant you not split the concerns,meaning run your backend on express and your frontend development with npm start (webpack-dev-server)? - KornholioBeavis
@user1185197 currently the frontend app has many api calls which requires the server to be running. That's why I am running the react app through the build folder. Is there a way to split that? - Sean sean
I dont understand? You have many api calls to several outside api's? If so thats fine. What are you actually doing on your backend that you require express? Some code would be nice - KornholioBeavis
@user1185197 for example, in the react app, an example api call would be like axios.post('/api/post/111'). When the server is running at localhost:8080, the request will be sent to localhost:8080/api/post/111. Did I do this part wrong? Is there a good way to decouple? - Sean sean

5 Answers

30
votes

Unfortunately this is something you will have to do yourself. You can use a tool like npm-watch to accomplish what you want though:

Install npm-watch

npm i --save-dev npm-watch

package.json

{
  "name": "react-app",
  "version": "0.1.0",
  "private": false,
  "devDependencies": {
    "npm-watch": "^0.1.8",
    "react-scripts": "0.9.5",
  },
  "dependencies": {
    "react": "^15.4.2",
    "react-dom": "^15.4.2"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject",
    "watch": "npm-watch"
  },
  "watch": {
    "build": "src/"
  }
}

Afterwards, just use npm run watch to start up npm-watch so it can rebuild your assets on changes.

Update:

React-scripts now includes a proxy option that proxies requests to a different host/port. For example, if your backend is running on localhost at port 9000 under the /api route, then you would add this line to your package.json: "proxy": "localhost:9000/api". You could then make requests as you normally would in production. (source: https://create-react-app.dev/docs/proxying-api-requests-in-development)

13
votes

While this doesn’t really answer your question, you shouldn’t be using npm run build in development. Not only the rebuilds are slow, but it also skips important React warnings for performance and size, so you’ll end up scratching your head more and getting a lot less details in the warnings.

If you just need to do API requests with Express, use the proxy feature which lets you proxy API requests from npm start to your server. There is also a tutorial with a matching repository demonstrating how to do that.

In production, of course, you should use the build produced by npm run build. But you would only need to run it before deployment.

0
votes

Run your backend on a different port. If your running on express modify the file bin/www

var port = process.env.PORT || 9000

and in your /src folder create a config file where you configure your api host,routes, params etc

//config/index.js
export const Config = {
   protocol: 'http',
   host: window.location.hostname, //or the environment variable
   params: '/api/',
   api: {post:'/posts/'}
}

and in your calling component or where ever your calling the api's

import {Config} from '../config'

axios.get(`${Config.protocol}${Config.host}${Config.params}${Config.api.posts}${some id i guess}`)
-1
votes

i am also using create react app, this is how i modified my scripts to run project for development(windows), build the production build and run the production build.

"scripts": {
    "start": "set PORT=8080 && react-scripts start",
    "build": "react-scripts build",
    "deploy": "set PORT=8008 && serve -s build"
  }

npm start : run project for development(windows) npm run-script build : build the production build npm run-script deploy: run the production build

  • npm install -g serve before run npm run-script deploy.
-6
votes

1> npm install create-react-app -g

2> create-react-app Your_Apps_Name