DEV Community

Error when serving React app from sub-folder

Quentin Sonrel on August 27, 2019

Hello there! I'm trying to serve a React application from a sub-folder. It's currently a very simple SPA running on the root URL (e.g. http://myse...
Collapse
 
jbutz profile image
Jason Butz

I was able to replicate your issue. The problem is serve. When you run serve -s build it starts the HTTP server. When you go to http://localhost:5000 in your browser it sends an HTTP request for / to serve. It will respond with ./build/index.html, which references assets from /newroot/static/..... But, there is no newroot directory inside your build directory. So it responds with ./build/index.html due to the -s flag.

If you rename the build directory to newroot and run serve . then you will be able to load your application at localhost:5000/newroot but navigation may not work correctly depending on what you are doing.

If you are trying to test the production deployment of your application you should do so in a production-like environment, not locally.

Collapse
 
anpos231 profile image
anpos231

Keep in mind that "homepage" is only used in production build.
Development still serves the app from "localhost:3000/"

Collapse
 
sudiukil profile image
Quentin Sonrel

Sure but as you can see in my post I'm serving the app in production mode. And this is where the error happens, nothing changes in dev mode.