dotenv flow

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

  • |

Lesson Transcript

  • [00:00 - 02:35] So in the last video we have talked about how to set one set of configurations, like if you have your local environment, what if you also want to do automated testing or test your production environment at your own machine? What do you do when you have multiple sets of configurations? In 1.2 we're going to talk about when, why, and how to handle this using a technique called dot n flow. So what is dot n flow? It's a strategy to determine which version of a variable to use when you have multiple configuration sets. It's actually featured a numerous front end tool chains, but also has a standalone library, which is what I'll be demonstrating today. As you can see, we're picking up from the end of the last lesson. And we 're going to actually go ahead and show you how to use it. First thing I'm going to do is actually install it. So you actually use this instead of dot n. So you'd first uninstall dot n. And then you would install dot n dash flow. You also configured in almost exactly the same way, where it says require dot n dot config, you just say dot n flow dot config. And it actually runs the same . Next thing I'm going to do is make a copy of my dot n. So we now have two configuration sets, I'm actually going to put at the end of the file dot n dot production. The way it works is when the value of node end matches the suffix of dot n dot whatever, that file is chosen. In this case, I only have the copy production. The standard environment names are test, production, and development. It's best to use these built in environment names that flow provides, because it's a convention that many libraries use, such as just, Babel, etc. And we'll go through those two in a bit. I've linked to the flow hierarchy documentation in the lesson docs below. However, you can technically use any name that you want. So the way to take advantage of multiple configuration sets is, of course, to have multiple different sets. So for now, I'm going to show you how this works. We have a use course value in our dot n. So in our dot n dot production, I'm going to set the value to true. I'm not going to change the server URL for now. And that's okay.

  • [02:36 - 03:33] We'll, we'll circle back to that. Next thing that I'll do is I'll go to my package JSON and make sure that there's two start scripts, one for production, and one, that's just without any, any node end value being set. Now let's see the difference and see what happens. If you run start, so that says undefined, served to all domains, which means that the use course is off. And then we'll do npm run start production. And it says exclusively served to local host. One problem with this approach is you may want to check in standard variables to get like the use course and not instant specific variables, like the server URL. What you can do is you can create a dot n dot local.

  • [03:34 - 03:50] So instead of overriding it here, I'll make a copy of dot n, call it dot n dot local. And I'll keep the server URL value and remove it from dot n dot production.

  • [03:51 - 04:08] This is a secure way to specify these local values because it is never supposed to be checked into get. Make sure you add it to your get ignore. And dot star dot local dot n dot local .

  • [04:09 - 04:49] Dot local takes even higher precedence than dot n. So even if you have values set in your dot n or your dot m dot production, the dot local will override that. You can also use a second dot local that is dot m dot production dot local. Although I'm not sure that's too necessary, you get a bit complicated, but it is a feature that you can use. We're not going to use that for this demo. Some of you are probably finding this whole flow process a little bit tedious and complicated.

  • [04:50 - 07:58] So let me explain why having multiple layers is useful. It's help. It isn't actually always useful. It isn't always helpful. I'd say there's only about two types of situations which it would help with. And for this lesson, let's call them a and b. A is for default. If you have default values that aren't secrets that you want to keep in your dot n file and plan on checking dot n into get flow would allow you to use the dot n dot local for setting the values of secrets during development. In production, you'll still be able to set the environment values through the dashboards of your automated deployments, which will go over in module two. It can also be useful if you have a lot of non-secret configuration values that differ between development and production, such as toggling a set of features for a beta environment. If you don't have multiple sets of defaults and you're doing automated deployments, you can already set environment variable values without dot and flow by setting the values in your dot n file for local testing , keeping it in dot getignore, and then for production, setting the real values through your deployment dashboard. Your secrets must not be checked in, so you will already have to set them wherever your code is being deployed, and you can toggle features through the same dashboard. And type B is for local development, local deployment. It's useful if you're generating bundles for deployment on your work station, such as for lambdas or front ends, mobile apps, where you need to be able to run, test, and deploy with different configuration sets on the same machine. This is an uncommon strategy for mature production environments with teams, as you'll likely use an automated deployment setup in the cloud, but it is still useful in initial product stages. If you're doing local deployment, but only have one set of configurations for both development and production, don't bother with dot and flow. Just use the regular dot n. So what else should be in your dot getignore? If you're using type B, local deployments. So if you're keeping secrets in your dot n dot production, or dot n dot development, those files must be in your dot getignore. You do not want to be checking in secrets. Anything that would be unsafe for the world to access should be ignored. And how do other people on your team know what to put in the dot n files? Well, so what happens when you have environment variables that you actually have values that you get ignored? Well, then you need to use what's called a dot end template. So just say we want to not we want we don't want to check in dot n dot production.

  • [07:59 - 09:31] We'll make a copy whoopsies copy of the file. dot production dot template. And this will be checked in. You can either put a junk value like an empty value, or you can put true, like you can put a default value. And then you check this into get and you get ignore dot ends dot production. So then what happens is that people have to make their own copy of the template. And then they would make their dot end dot production based on that. There's also some libraries that use dot example, instead of dot template. But they're the same thing. Make sure to describe in your read me all the variables, be nice to your team, document it, and make sure to describe step by step instructions on dot and set up in your project read me as well. Because not everybody will understand what dot end is and why it's awesome. One final topic before we move on is do you need to validate your dot and files? Well, if your dot and files are get ignored, how do other teammates know what the types of the value should be? And if you add a new variable to your code, how do you make sure that others will add it to their local configuration files properly?

  • [09:32 - 10:08] You probably won't even know that they have to use it now. Well, there is a library called dot and extended. I talk about that in the description. I don't think you need it though. This testing is not done on production servers. It's for development or automated testing tools where environment variables will not be set with dot and files. What I recommend you do in setup dot end validation is use the flow hierarchy to set the defaults.

  • [10:09 - 10:28] Explain your inputs clearly in your read me. Use managed dot and food not dot and tools, which we 'll be talking about later on. Communicate to your team when you make changes to the structures of your dot and files. That's all for now. Have a great day.