Can't build create-react-app project with custom PUBLIC_URL


I'm trying

PUBLIC_URL= npm run build

with a project built using the latest create-react-script.

However, the occurrences of %PUBLIC_URL% in public/index.html are replaced with an empty string, not the expected value PUBLIC_URL.

public/index.html contains code like

<script src="%PUBLIC_URL%/static/js/jarvis.widget.min.js"></script>

Hours of searching the internet and stack overflow show that very little is written about PUBLIC_URL. I cloned create-react-app from GitHub and have been browsing the code but have not yet been enlightened.

Does anyone have any suggestions as to what I'm doing wrong?

This question is tagged with javascript reactjs create-react-app

~ Asked on 2017-03-09 03:32:02

The Best Answer is


If the other answers aren't working for you, there's also a homepage field in package.json. After running npm run build you should get a message like the following:

The project was built assuming it is hosted at the server root.
To override this, specify the homepage in your package.json.
For example, add this to build it for GitHub Pages:

  "homepage" : "",

You would just add it as one of the root fields in package.json, e.g.

  // ...
  "scripts": {
    // ...
  "homepage": ""

When it's successfully set, either via homepage or PUBLIC_URL, you should instead get a message like this:

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

~ Answered on 2017-10-02 00:24:05


People like me who are looking for something like this in in build:

<script type="text/javascript" src="">

Then setting to homepage in package.json will not work.

1. Quick Solution

Build your project like this:

set PUBLIC_URL= run build


PUBLIC_URL= npm run build

And you will get

<script type="text/javascript" src="">

in your built index.html

2. Permanent & Recommended Solution

Create a file called .env at your project root(same place where package.json is located).
In this file write this(no quotes around the url):


Build your project as usual (npm run build)
This will also generate index.html with:

<script type="text/javascript" src="">

3. Weird Solution (Will do not work in latest react-scripts version)

Add this in your package.json
"homepage": "http://://",

Then index.html will be generated with:

<script type="text/javascript" src="//">

Which is basically the same as:

<script type="text/javascript" src="">

in my understanding.

Github Issue Github Comment

~ Answered on 2019-01-08 13:23:51

Most Viewed Questions: