When developing web applications by using Create React App, developers get NODE_ENV=development on their local environment and NODE_ENV=production ...
For further actions, you may consider blocking this person and/or reporting abuse
How to run it?
i make env.local
it contains
this run script in npm
when i run
it fails and load to .env
@consciousnessdev Thanks for your comment.
You should add
REACT_APP_
prefix to your all custom env variables. eg,REACT_APP_CUSTOM_NODE_ENV
.the command is
npm run start:local
according to your scripts, just missing:
.Sorry for making this mistake, and I will update the article.
Again, thanks for your patient.
Best,
Donghyuk (Jacob) Jang
[Update] But when i run it with your suggestion, it still have an error
I have tried in the example repository, and it seems to be working fine. You may have some conflicts.
The example repository is github.com/DonghyukJacobJang/cra-e...
You will be able to find
.env.local
andstart:local
inpackage.json
.Since I cannot see all your config and settings, providing you the example repo is the best thing I can do at the moment.
Please let me know if you have questions.
Best,
Donghyuk (Jacob) Jang
Pardon Me, i'd typo on 'react-script', which is true 'react-scripts', with 's' in the end of script it, thanks a lot! :O
Thanks Donghyuk. One thing to note here is that, while my
.env.staging
is loaded,process.env.NODE_ENV
is set toproduction
despite theNODE_ENV=staging
setting in.env.staging
Is this consistent with your experience?
Seems so based on create-react-app.dev/docs:
Assuming true, what's the value in adding
NODE_ENV=staging
to.env.staging
?I will test this out ASAP, but I remember that I had to create a custom var to distinguish environments in javascript code.
Nice post, Jacob! thanks!
It's a great post! Thank you. I needed this.
I am very glad that it helps you :) Thank you so much for your message.
I think, env-cmd should be installed by
$ yarn add -D env-cmd
Thank you for the article!
I was initially confused as I was not aware that "start" and "build" scripts use ".env.development" and ".env.production", respectively, by default.
I am using visual studio 2017 with React template
in which I followed above steps but its not working for staging
can you add working source code also
There was
env-cmd
version update and it required a parameter to point a customenv
file, As @foxbit19 discovered.CRA template with
env-cmd
example is available at github.com/DonghyukJacobJang/cra-e... .Thank you so much @sushantraje2000
Thanks for your article! I think
env-cmd
needs-f
argument to load the custom file correctly. Otherwise it fails and load.env
file by default.Thanks for this information. When this article was written, it was using
env-cmd
version 8.0.2. It was not required-f
option. I also just tested the latest version which is 9.0.3 without the parameter and caused an issue. As a result, when pointing a customenv
file, it is required. Thank you so much @foxbit19 .By far the simplest solution. Good stuff!
Much thanks, it works.
In addition to using environment variables I can recommend the tool github.com/dotenv-linter/dotenv-li... - it’s a lightning-fast linter for .env files.
Maybe it would be useful for you.
Thanks, this work flawless.
Will it apply with the yarn build command?