I'm trying
PUBLIC_URL=http://example.com 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?
People like me who are looking for something like this in in build:
Then setting
https://dsomething.cloudfront.net
tohomepage
inpackage.json
will not work.1. Quick Solution
Build your project like this:
(windows)
(linux/mac)
And you will get
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:
3. Weird Solution
Add this in your package.json
"homepage": "http://://dsomething.cloudfront.net",
Then index.html will be generated with:
Which is basically the same as:
in my understanding.
Github Issue Github Comment
This problem becomes apparent when you try to host a react app in github pages.
How I fixed this,
In in my main application file, called
app.tsx
, where I include the router. I set the basename, eg,<BrowserRouter basename="/Seans-TypeScript-ReactJS-Redux-Boilerplate/">
Note that it is a relative url, this completely simplifies the ability to run locally and hosted. The basename value, matches the repository title on GitHub. This is the path that GitHub pages will auto create.
That is all I needed to do.
See working example hosted on GitHub pages at
https://sean-bradley.github.io/Seans-TypeScript-ReactJS-Redux-Boilerplate/
Not sure why you aren't able to set it. In the source,
PUBLIC_URL
takes precedence overhomepage
You can try setting breakpoints in their code to see what logic is overriding your environment variable.
If the other answers aren't working for you, there's also a
homepage
field inpackage.json
. After runningnpm run build
you should get a message like the following:You would just add it as one of the root fields in
package.json
, e.g.When it's successfully set, either via
homepage
orPUBLIC_URL
, you should instead get a message like this:As documented here create-react-app will only import environment variables beginning with
REACT_APP_
, so thePUBLIC_URL
, I believe, as mentioned by @redbmk, comes from thehomepage
setting in thepackage.json
file.That is not how the PUBLIC_URL variable is used. According to the documentation, you can use the PUBLIC_URL in your HTML:
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
Or in your JavaScript:
The PUBLIC_URL is not something you set to a value of your choosing, it is a way to store files in your deployment outside of Webpack's build system.
To view this, run your CRA app and add this to the
src/index.js
file:console.log('public url: ', process.env.PUBLIC_URL)
You'll see the URL already exists.
Read more in the CRA docs.