-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A way to define custom environment variables #102
Comments
Can you please clarify your use case in a little more detail? I'm specifically interested in the problem you were solving (configuring for multiple quizzes?) than the exact proposed solution. |
I might have one. Let's say there is api server running at different enviroments like so:
And I'm building SPA which supposed to use CORS and being hosted anywhere, just like static site. And such I want to supply dev/prod absolute paths for different versions of SPA. Hope this makes sense. |
I think at that point you have a very advanced use case and ejecting is the way to go. I don't think adding |
@mxstbr Yes may be true, but I just wanted to open this for some discussion, please feel free to close it. I try to explain my use which is somewhat similar to @dmi3y's usage:
Basically the This is an advanced use case but not a obtrusive solution (not needed but can be provided kind of thing). I hope this explains clearly the purpose. |
Absolutely no fault in that, we're definitely not saying I'm not sure I fully understand your use case, wouldn't you need access to the webpack config anyway if you're changing the |
Something I missed when trying I was able to achieve that by manually changing the |
^ That's also my use case. |
I'm curious, can you explain how the workflow works with cloud9? Do you have the developer server running in the cloud and use their browser text editor to edit the code? Where do you see the terminal output? Why not develop locally? |
I mainly use this kind of service (Cloud9, Nitrous.io, etc...) when I'm at work and don't have access to my PC. They're also a good place to study with friends, since they allow collaborative coding. Plus, somewhere free to showcase a small app? And yes, you use their browser text editor to edit the code and there's also a terminal emulator in the text editor. |
One use case I often have for environment variables is when I want to run some code in production but not in development. For example, if you're using an analytics service, you might not want to log any analytics from development versions of the app. Or if you use an external service to sign people up for a mailing list, you don't want your test accounts getting in there. About Cloud9, one situation I see it adopted more and more is for education. If you're teaching a class of people how to use some technology, you don't want to spend a ton of time making sure everyone has their development environment set up. If you can just give them one Cloud9 config they can get going quickly. So it would be pretty nice to support Cloud9. They also just got acquired by Amazon to be integrated into AWS, which is another sign this sort of thing is going to grow in popularity. |
We currently already allow this with
Absolutely agree. I just want us to learn more about different use cases first, and approach the problem holistically, rather than add flags one by one over time. A holistic solution might turn out to be simpler. |
Hmm good point. Here's a slightly different example - let's say your app accesses a Firebase backend. There is one that's only used for production, but each developer has their own for local development, so you can stick random experimental stuff in your database without it borking other peoples' environments. One pattern I would use for this is to check an environment variable to get access info for the backend, and have a startup script for development mode that grabbed this from a config file that isn't checked into source control. Is there currently a straightforward way to do this within a |
Maybe I'm missing something here, but what about defining two different scripts in your {
"dev": "SOME_ENV_VAR='development' npm start",
"prod": "SOME_ENV_VAR='production' npm start"
} and then I think in your app you could have a if (process.env.SOME_ENV_VAR === 'development') {
// export different stuff here
} else {
} Not 100% sure how a |
The problem I encountered is that IP and PORT are hard coded on the start script, which isn't exactly in the app code. So just defining some environment variables before running the server is not enough. |
This currently won’t work because we don’t pass all ENV variables to |
We definitely wouldn’t be hardcoding them when we get to solving this problem. I’m just saying I want to approach this holistically and not just add a bunch of variables one by one without any cohesive system. Let’s figure out a generic enough system that lets us configure |
Sometimes, like with Code-Push, you have additional constellations like this:
So you would need another ENV variable like |
Does DefinePlugin dumps all the environment variables in a js object in the generated output or does a find/replace of all of them? There are a lot of very sensitive information in environment variables from a security perspective, this is a big attack vector and we don't want to dump it all by default. |
It does a find and replace. Not only is that important for security, it also ensures that dead-code-elimination works properly after the environment variables are set. On the other hand, I would probably not want any npm module I install to be able to expose any of my environment variables, that seems like an auditing nightmare. My inclination would be to do NODE_ENV for all modules (including npm) but maybe do custom environment variables only in application code. |
We could choose a namespace, like |
Maybe this could be solved instead with dynamic imports - if you do something like
then you could get the effect of environment variables without needing changes in the current runtime. |
@lacker the problem is that NODE_ENV is effectively reserved for optimisation. You can't use anything other than NODE_ENV. I once built an app for one client, and ended up deploying a second identical copy of the app for a different client. The only difference between the two deployments was the company names and branding colours. To do this, I just defined environment variables for each of those features. The issue is that those features vary between different clients, not between development and production. |
@gaearon I don't like the idea of a prefix. It feels awkward to have to prefix all the variables with something that has nothing to do with my app, and counter-intuitive that some variables pass through and others do not. |
Can we use |
By the way I’m coming to the conclusion that specifying ports, proxies and stuff is fair game because we’re competing for system resources. We just need to make sure the defaults work well with zero configuration, but some optional config for shared resources like ports is fine. |
Took me half an hour to find out this: |
@thg303 and posterity, note that to use a port below 1024, you'll need to |
@thg303 Did it work for you just by adding the .env file with the env variable? I'm having trouble getting that to work. |
@eric-khoury I just test it for changing the port number. I have no idea for other env variables. |
@eric-khoury Please note that only env that starts with |
@dvkndn Thx for the info, I missed that part completely. |
Simple question @ALL, how can we inspect the value of NODE_ENV in the front-end React code? I assume this is passed to the front-end given this conversation? Is there a way to set a default NODE_ENV on the server as well? Perhaps by editing the .env file? I assume you have to npm run eject to do that? Or not? |
You could always just alias it with the REACT_APP_ prefix if it's not available
…________________________________
From: Operations Research Engineering Software+ <notifications@github.com>
Sent: Dec 12, 2016 9:53 PM
To: facebookincubator/create-react-app
Cc: Ryan Fairchild; Mention
Subject: Re: [facebookincubator/create-react-app] A way to define custom environment variables (#102)
Simple question @ALL<https://github.com/all>, how can we inspect the value of NODE_ENV in the front-end React code? I assume this is passed to the front-end given this conversation?
-
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub<#102 (comment)>, or mute the thread<https://github.com/notifications/unsubscribe-auth/AADdT4RzoEf5ogcwNpKbKnzb05z034rtks5rHgi0gaJpZM4JTJJ0>.
This e-mail message, and any attachments, is intended only for the use of the individual or entity identified in the alias address of this message and may contain information that is confidential, privileged and subject to legal restrictions and penalties regarding its unauthorized disclosure and use. Any unauthorized review, copying, disclosure, use or distribution is strictly prohibited. If you have received this e-mail message in error, please notify the sender immediately by reply e-mail and delete this message, and any attachments, from your system. Thank you.
|
@ORESoftware could you use |
@ORESoftware you have to set NODE_ENV via os environment variable. in linux it would be like executing this command in the terminal: |
@thg303 thanks, I got that part :) what I am not sure of, is if this value* is sent by create-react-app or if I have to do that manually. *or any other env values besides NODE_ENV If it is sent to the front-end, how can I access the value for NODE_ENV on the front-end sent from the backend? Basically looking to render things differently depending if I am dev or prod, etc. |
Have you had a chance to read the corresponding section in the User Guide? It specifically answers this question with an example. TLDR: it’s
You can't "send" it from backend. If it's on the backend, your app is already compiled in production mode, so it's
This answer is incorrect. With Create React App |
@gaearon so basically in short, you can do:
and on the front end, we have: const nodeEnv = window.REACT_APP_NODE_ENV` // (likely incorrect) but looking at the link, you sent, it looks like: render() {
return (
<div>
<small>You are running this application in <b>{process.env.NODE_ENV}</b> mode.</small>
<form>
<input type="hidden" defaultValue={process.env.REACT_APP_SECRET_CODE} />
</form>
</div>
);
} that would mean that instead of const nodeEnv = window.REACT_APP_NODE_ENV` // front-end instead on the front-end we have const nodeEnv = process.env.NODE_ENV` //front-end It's a little counterintuitive to have Node.js related stuff on the front-end, but I guess that's pretty normal now that we are using Webpack etc. We have to use surrogates for some of the backend code (like process, fs, etc). thanks |
No, var env = process.env.NODE_ENV; As I already mentioned, User Guide contains an example that specifically shows this. I recommend giving it another look. For any custom variables, you'd use Please refer to the section in User Guide, I'm just repeating what it says here. |
@gaearon ok I just read what you wrote, np. However the user guide does not seem to demonstrate how to access the variables in the front-end, at least not in the same section (tell me if I am wrong :) I have this "start-dev": "REACT_APP_NODE_ENV=development && react-scripts start", on the front-end if I log process.env, I get an empty object. So I am guessing that instead of doing process.env.REACT_APP_NODE_ENV it will be window.REACT_APP_NODE_ENV I will test it out, but if it's it not in the docs yet, I would be happy to add whatever the right way to do this is. e.g. window.R = no search results |
I just filed a new issue for this, sorry if it's my misunderstanding |
As I wrote in the other two comments (#102 (comment) and #102 (comment)) you don't need to specify console.log(process.env.NODE_ENV); You don't need anything else for it to work. Do you have any problems getting this one working? |
I think looking at the source might help anyone that url might get old, but that's the idea |
Shouldn't it be possible to edit vars at runtime using the dev settings? |
fwiw - here's what I did for this:
Above works for dev process as well. |
Sorry, I know this is closed, but I was wondering if CRA can be set up to use a custom It would be handy if an additional environment variable could be specified to select the custom I'm not sure if this is too advanced a use case, but it seemed like having multiple stages of development with different environments each might be quite common. |
I was just reading through this thread, because I needed the same thing ( I think this is what some are looking for, right? |
@bradwestfall Kind-of, My suggestion for a change would be to check if an environment variable called |
Yes, support for @Baldeep Sorry we didn't make it clear. If you just use I will lock this thread to prevent further confusion. |
Oops, I misread your message @Baldeep. You're right So if you feel this is useful please file a new issue. |
This may be a tiny yet non-obtrusive solution. Allow a custom
.env.js
file that exports a key value pair used for custom webpack defines. I guess this wont be useful for many, since you any way get to refer to NODE_ENV but I also used this for defining a publicPath from an environment variable or defaulting to root. (useful for me as i had to run the same app for multiple quizzes, so I could build the frontend without configuring anything and having sane default not-get-in-the-way configs while development and testing)The text was updated successfully, but these errors were encountered: