[Update] But when i run it with your suggestion, it still have an error
npm run start:local
> apps@0.1.0 start:local /home/me/react/apps
> env-cmd -f .env.local react-script start
spawn react-script ENOENT
Hangup
npm ERR! code ELIFECYCLE
npm ERR! errno 129
npm ERR! apps@0.1.0 start:local: `env-cmd -f .env.local react-script start`
npm ERR! Exit status 129
npm ERR!
npm ERR! Failed at the apps@0.1.0 start:local script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/me/.npm/_logs/2019-12-04T01_34_08_907Z-debug.log
Thanks Donghyuk. One thing to note here is that, while my .env.staging is loaded, process.env.NODE_ENV is set to production despite the NODE_ENV=staging setting in .env.staging
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 custom env file, it is required. Thank you so much @foxbit19
.
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.
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:localaccording 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.localandstart:localinpackage.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.stagingis loaded,process.env.NODE_ENVis set toproductiondespite theNODE_ENV=stagingsetting in.env.stagingIs this consistent with your experience?
Seems so based on create-react-app.dev/docs:
Assuming true, what's the value in adding
NODE_ENV=stagingto.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.
Thanks for your article! I think
env-cmdneeds-fargument to load the custom file correctly. Otherwise it fails and load.envfile by default.Thanks for this information. When this article was written, it was using
env-cmdversion 8.0.2. It was not required-foption. 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 customenvfile, it is required. Thank you so much @foxbit19 .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-cmdversion update and it required a parameter to point a customenvfile, As @foxbit19 discovered.CRA template with
env-cmdexample is available at github.com/DonghyukJacobJang/cra-e... .Thank you so much @sushantraje2000
By far the simplest solution. Good stuff!
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.
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.
Much thanks, it works.
Thanks, this work flawless.
Will it apply with the yarn build command?