2

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!

8
  • for your project to read .env variables you will need first dotenv npm module and then you would read like process.env.PUBLIC_URL assuming in your .env file you have a variable with PUBLIC_URL 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. Commented Jul 13, 2018 at 11:16
  • this PUBLIC_URL is used where ?, I am unable to understand where you are using this variable 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.com/facebook/create-react-app/blob/master/packages/…) 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/… Commented Feb 22, 2022 at 9:46

1 Answer 1

4

I found the solution. I needed to do PUBLIC_URL=/new-manager-staging/ instead of PUBLIC_URL=https://xxxxx.io/new-manager-staging.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.