JillAndMe
JillAndMe

Reputation: 4551

How to run production react app in local with local back-end

I have seen using production react app with server in local.

But mostly I found the way is running production react app using express.js with Heroku.

How to running production react app with server in local without any change of code?

Upvotes: 31

Views: 59897

Answers (6)

Simran Singh
Simran Singh

Reputation: 2869

You can follow these commands

If you are using NPM:

  1. npm run build
  2. npm install -g serve
  3. npx serve -s build

And if you are using Yarn:

  1. yarn build
  2. yarn global add serve
  3. npx serve -s build

Upvotes: 1

Ankit Tiwari
Ankit Tiwari

Reputation: 1239

As of date by default for CRA

serve -s build

will deploy your app on localhost port 5000

If you want to open on custom port use flag -l

serve -s build -l customportnumber

CRA Deployment Official Docs

Upvotes: 7

Monika Mangal
Monika Mangal

Reputation: 1760

When you run npm run build your console should actually say something like the following

The build folder is ready to be deployed.
You may serve it with a static server:

npm install -g serve
serve -s build

The build script is building your entire app into the build folder, ready to be statically served. However actually serving it require some kind of static file server, like the one they propose.

After running the command serve -s build you can access your production build at localhost (on the specified port).

You can of course run whatever static file server you like, I usually use express for this, however serve seems like the easiest option to just serve your statics files with a single command.

Upvotes: 58

footyapps27
footyapps27

Reputation: 4042

There are various ways of achieving this.

You can use http-server or serve packages for running local servers. If on MacOS, you can also use python SimpleHttpServer to host the production build folder.

Reference: https://medium.com/@samratshaw/test-react-production-build-locally-434907be9e49

Upvotes: 1

Usama Tahir
Usama Tahir

Reputation: 1797

make a production build of the react app locally, (run npm run build if you are using create react app) and then you can use serve to run it locally by running serve -s build. build is the folder name of your production build.

Upvotes: 3

If you want to run you React application on default :8000 port it's more convenient to create an nginx proxy server which will run on port :8000 and proxy your request into your express application which is serving your bundled application running on port for example :8081 or whatever port you are using.

Upvotes: 0

Related Questions