I'm a newbie here with web-development and recently started pushing my simple web-app to a GitHub page. I quickly discovered that my page won't render after I integrated react-router
with it because my dev and prod URL links are different.
My question is, how can I set up my package.json
and .env
such that it will correctly render my URLs?
My .env
file looks like this:
REACT_APP_PUBLIC_URL="my-site"
and my package.json
looks like:
"scripts": {
"start": "NODE_ENV=development react-scripts start",
"start:prod": "NODE_ENV=production react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject",
"predeploy": "npm run build",
"deploy": "gh-pages -d build"
}
Inside my Index.js
, I'm doing this:
if (process.env.NODE_ENV === "production") {
require("dotenv").load();
}
console.log("PROCESS.ENV.NODE_ENV: ", process.env.NODE_ENV); // This prints "development"
console.log("PROCESS.ENV.PUBLIC_URL: ", process.env.REACT_APP_PUBLIC_URL); // This prints "my-site"
When I run npm run start:prod
, I would think that it sets the NODE_ENV
to production
but it doesn't seem to be doing that.
Basically, what I want to do is during development, my process.env.PUBLIC_URL
should be ""
and during production, it should be "my-site"
. That way, my router can correctly render the corresponding views. Thanks for your help!