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?
If you really need create-react-app , you might need to reinstall Node and reconfigure your dependencies to ensure you have a fresh start with Node, npm, npx, and the like.
To reference assets in the public folder, you need to use an environment variable called PUBLIC_URL . Inside index.html , you can use it like this: <link rel="icon" href="%PUBLIC_URL%/favicon.ico" /> Only files inside the public folder will be accessible by %PUBLIC_URL% prefix.
To solve the React. js error "You are running create-react-app 4.0. 3, which is behind the latest release (5.0. 0)", run the npx clear-npx-cache command and re-run your app creation command, e.g. npx create-react-app my-app .
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" : "http://myname.github.io/myapp",
You would just add it as one of the root fields in package.json
, e.g.
{ // ... "scripts": { // ... }, "homepage": "https://example.com" }
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 https://example.com. You can control this with the homepage field in your package.json.
People like me who are looking for something like this in in build:
<script type="text/javascript" src="https://dsomething.cloudfront.net/static/js/main.ec7f8972.js">
Then setting https://dsomething.cloudfront.net
to homepage
in package.json
will not work.
Build your project like this:
(windows)
set PUBLIC_URL=https://dsomething.cloudfront.net&&npm run build
(linux/mac)
PUBLIC_URL=https://dsomething.cloudfront.net npm run build
And you will get
<script type="text/javascript" src="https://dsomething.cloudfront.net/static/js/main.ec7f8972.js">
in your built index.html
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):
PUBLIC_URL=https://dsomething.cloudfront.net
Build your project as usual (npm run build
)
This will also generate index.html with:
<script type="text/javascript" src="https://dsomething.cloudfront.net/static/js/main.ec7f8972.js">
Add this in your package.json
"homepage": "http://://dsomething.cloudfront.net",
Then index.html will be generated with:
<script type="text/javascript" src="//dsomething.cloudfront.net/static/js/main.ec7f8972.js">
Which is basically the same as:
<script type="text/javascript" src="https://dsomething.cloudfront.net/static/js/main.ec7f8972.js">
in my understanding.
Github Issue Github Comment
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With