I am working on a project built with create-react-app using two homepage url (staging and production). To avoid the need to change homepage in package.json manually before deployment to staging or production, I tried to set a PUBLIC_URL environment variable in both my .env and .env.production files and I deleted homepage in package.json.
PUBLIC_URL=
After reading the doc, I assumed it would set the homepage url during build time, but it does not seem to be the case.
Is there an other place to set PUBLIC_URL or an other way to have multiple homepages or to set homepage dynamically in package.json? Thanks in advance!
I am working on a project built with create-react-app using two homepage url (staging and production). To avoid the need to change homepage in package.json manually before deployment to staging or production, I tried to set a PUBLIC_URL environment variable in both my .env and .env.production files and I deleted homepage in package.json.
PUBLIC_URL=https://xxxxx.io/new-manager-staging
After reading the doc, I assumed it would set the homepage url during build time, but it does not seem to be the case.
Is there an other place to set PUBLIC_URL or an other way to have multiple homepages or to set homepage dynamically in package.json? Thanks in advance!
Share Improve this question edited Jul 15, 2022 at 19:00 dbc 118k26 gold badges264 silver badges387 bronze badges asked Jul 13, 2018 at 10:35 Matthieu StadelmannMatthieu Stadelmann 811 silver badge8 bronze badges 8-
for your project to read .env variables you will need first
dotenv npm module
and then you would read likeprocess.env.PUBLIC_URL
assuming in your .env file you have a variable with PUBLIC_URL – warl0ck Commented Jul 13, 2018 at 10:59 - I can read environment variables, that's not the problem (I use dotenv as well). The problem is that when I delete homepage in package.json and use PUBLIC_URL instead so I can have a homepage for staging and a homepage for production, PUBLIC_URL isn't used as homepage, so basically I can't access my website. – Matthieu Stadelmann Commented Jul 13, 2018 at 11:16
- this PUBLIC_URL is used where ?, I am unable to understand where you are using this variable – warl0ck Commented Jul 13, 2018 at 11:18
- 1 No, I can read them. To me it seems that I can't just use PUBLIC_URL instead of homepage in package.json, that's why I am asking if anyone else knows how to set homepage dynamically (github./facebook/create-react-app/blob/master/packages/…) – Matthieu Stadelmann Commented Jul 13, 2018 at 11:50
- 1 The link to docs how to setup .env files has moved here: create-react-app.dev/docs/deployment/… – sidon Commented Feb 22, 2022 at 9:46
1 Answer
Reset to default 4I found the solution. I needed to do PUBLIC_URL=/new-manager-staging/
instead of PUBLIC_URL=https://xxxxx.io/new-manager-staging
.