7

In my package.json file I have "homepage" : "http://myname.project.com/myapp",

I build the app using npm run build and the output confirms that a /build directory was created. Output:

102.97 KB build/static/js/main.bfa46b52.js

24.37 KB build/static/css/main.cfe8a47e.css

The project was built assuming it is hosted at /myapp/. You can control this with the homepage field in your package.json.

The build folder is ready to be deployed.

If I start the app using npm start or serve -s build the app will start and the index.html page will load, but the references to static resources from index.html are broken (/myapp/static/js/main.js and /myapp/static/css/main.css). I can navigate directly to to them without the /myapp (localhost:5000/static/js/main.js works; localhost:5000/myapp/static/js/main.js does not work)

If I deploy the build folder to a different plain old apache server under a /myapp directory it seems to work as expected. Is there any way I can configure serve -s build to serve up pages from a /myapp directory?

The app is currently one page with no links and no implementation of react-router or anything equivalent. The team had been using webpack, but removed it. The app gets bundled using react-scripts.

Community
  • 1
  • 1
user1521567
  • 1,653
  • 2
  • 20
  • 31
  • same unanswered issue: https://stackoverflow.com/questions/43011207/using-homepage-in-package-json-without-messing-up-paths-for-localhost?rq=1 – user1521567 Aug 16 '17 at 03:44

1 Answers1

3

It wasn't clear to me that two things were happening.

  1. Setting the homepage attribute made modifications to index.html so that it would behave properly when deployed to /myapp directory.
  2. Running npm start or serve -s build/ still deployed my application from the root directory of the built in static server.

The homepage attribute doesn't have anything to do with where your app actually gets deployed. To address this issue I used a postbuild script move the contents of my /build folder to /build/myapp folder. Then it works as expected.

So more discussion about this here: https://github.com/facebookincubator/create-react-app/issues/1354#issuecomment-311506599

user1521567
  • 1,653
  • 2
  • 20
  • 31