create-react-app npm run start in production mode. Maybe not possible?

17,394

Solution 1

The best option is probably to do a normal production build and then run it locally.
First install an HTTP server:

npm install serve -g

Then:

npm run build
serve -s build

By default, it will run on port 5000 so your local URL is http://localhost:5000

Solution 2

To serve the app in production mode you need to follow below steps

  1. create a production build

    npm run build

  2. install npm serve globally

    npm i -g serve

  3. You may serve it with a static server now

    serve -s build

You can check for more options here.

For the development in production you can enable Hot reloading in react app without ejecting

With just 3 lines of code, you can turn on HMR, but with one big caveat: React state and DOM state are not preserved between reloads. This is kind of a bummer.

You can add these lines to index.js to turn on standard Webpack HMR that doesn’t preserve state between reloads:

if (module.hot) {
  module.hot.accept();
}

Read more about it here Hope it helps Thanks!

Share:
17,394
Admin
Author by

Admin

Updated on July 13, 2022

Comments

  • Admin
    Admin almost 2 years

    I need to run the server like I simply do with:

    npm run start

    but I need to use the production mode. Is this possible?

    In ember or angular it is possible. How to do in create-react-app?

    I tried npm run start --prod but nothing.