最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Setting PUBLIC_URL in different .env files (create-react-app) - Stack Overflow

programmeradmin1浏览0评论

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 like process.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
 |  Show 3 more ments

1 Answer 1

Reset to default 4

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

发布评论

评论列表(0)

  1. 暂无评论