Hello i have a reactjs app, and I build my project with bellow command
npm build
Here is my package.json
file:
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"},
after build i have folder with build files and index.html file But all paths in this .html are absolute, i want to build with relative path
for example (index.html): now i have:
<script type="text/javascript" src="/static/js/main.af2bdfd5.js"></script> <link href="/static/css/main.097da2df.css" rel="stylesheet"> <link rel="shortcut icon" href="/favicon.ico">
i want this:
<script type="text/javascript" src="./static/js/main.af2bdfd5.js"></script> <link href="./static/css/main.097da2df.css" rel="stylesheet"> <link rel="shortcut icon" href="./favicon.ico">
As mentioned in a comment, React's documentation covers this topic:
https://facebook.github.io/create-react-app/docs/deployment#building-for-relative-paths
Facebook recommends to install the tool env-cmd, create a file with an environment variable and a script in package.json to run.
That's a good concept but unfortunately, this fix does not work properly for two reasons.
First, env-cmd requires the path to start with
./
:Second, I'm not sure what the environment variable
REACT_APP_API_URL
is being used for but at least in create-react-app it'sPUBLIC_URL
. Creating a file named.env.staging
with the following content solved the issue for me:I think the creators of build tools should make it easier to deploy to a subfolder.