admin管理员组文章数量:1355540
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!
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!
- works on my machine, question, did you save the file? – rc_dz Commented Sep 28, 2018 at 22:32
-
@rc_dz Yes, I saved it. I also realized I made a typo in the original post and edited it. The
console.log(process.env.REACT_APP_PUBLIC_URL)
prints"my-site"
butprocess.env.NODE_ENV
printsdevelopment
when I expect it to printproduction
even though I executenpm run start:prod
– Tim Commented Sep 28, 2018 at 22:34 -
another question, when calling
npm run start:prod
, are you sure there is no space or type betweenstart
andprod
? If you type is asnpm run start prod
ornpm run start :prod
it will run the same asnpm run start
– rc_dz Commented Sep 28, 2018 at 22:38 -
@rc_dz Correct, I'm not putting a space between
start
andprod
I'm runningnpm run start:prod
– Tim Commented Sep 28, 2018 at 22:39 -
can you try switching to :
"start": "NODE_ENV=production react-scripts start", "start:prod": "NODE_ENV=development react-scripts start",
and see what happens – rc_dz Commented Sep 28, 2018 at 22:41
3 Answers
Reset to default 5React-scripts sets NODE_ENV automatically, see article in medium..
One of the options is using different env variables, case MY_APP_ENV
It's possible that react-scripts
is just overriding it to be development
. Here is some source I dug up :O
https://github./facebook/create-react-app/blob/master/packages/react-scripts/scripts/start.js#L11
Perhaps options you can consider is use a different env variable (you can define whatever you want quite honestly. Or you can just leave the production
build to react-scripts
/create-react-app
since it seems like they have some internal logic to do this for you.
Create .env.qa1
and add build script:
"build-qa1": "sh -ac '. .env.qa1; react-scripts start'"
本文标签: javascriptPackagejsonNODEENV to proddeploymentStack Overflow
版权声明:本文标题:javascript - Package.json - NODE_ENV to proddeployment - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743939255a2565257.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论