As mentioned in a comment, React's documentation covers this topic:

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 ./:

"scripts": {
    "build:staging": "env-cmd -f ./.env.staging yarn run build"

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's PUBLIC_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.


If you're using webpack, you can try setting publicPath to ./

You can read more about it here.


I encountered a similar issue and resolved it by setting "homepage": "./" in package.json

I found this solution here


// package.json
  "name": "your-project-name",
  "version": "0.1.0",
  "homepage": "./", # <--- Add this line ----

Run npm run build again.

This will change the path to ./, which is the relative path of your project.

Related Query

More Query from same tag