Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 959
Gulliver Smith Avatar asked Mar 09 '17 03:03

Gulliver Smith


People also ask

Why react app is not creating?

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.

What is Public_url in index HTML?

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.

How do you fix you are running create react App 4.0 3 which is behind the latest release 5.0 0?

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 .


2 Answers

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. 
like image 128
redbmk Avatar answered Sep 16 '22 17:09

redbmk


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.

1. Quick Solution

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

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):

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"> 

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

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

like image 40
Vaibhav Vishal Avatar answered Sep 18 '22 17:09

Vaibhav Vishal