over 5 years ago Syntax Podcast
Hasty Treat - Environmental Variables
- 00:03 Today's podcast will cover environmental variables
- 00:26 Scott introduces himself and Wes Bos
- 00:54 Wes jokingly calls himself the "variable master"
- 01:05 Today's sponsors are Sanity and Sentry
- 02:32 Sanity overview and details
- 02:50 Sentry overview
- 03:57 What are environment variables?
- 05:40 Env vars are not checked into source control
- 06:37 .env files store env vars locally
- 09:31 Encrypted env vars are more secure
- 10:14 Use both plain text and encrypted env vars
- 10:58 Env var groups help organize across services
- 13:37 Prefix client-side env vars differently
- 15:15 Hosts provide useful dynamic env vars
- 16:16 Storing env vars in 1Password is common
- 17:50 Multiple .env files can be useful
- 19:02 Set env vars directly in npm scripts
- 19:31 crossenv provides cross-platform env vars
- 20:10 node_options sets CLI flags for node
- 22:12 node --inspect opens Chrome DevTools
- 22:52 Next show will cover building authentication
crossenv provides cross-platform env vars
Wes Bos
Just make it work across
Wes Bos
and I use this in a couple of my courses because This works great. What you just explained works great in Unix are variables that are based on which environment is it running in. So what are different environments? Well, you have, like, development when you're running it locally on your machine. You probably wanna connect to some sort of test database, and then you push that thing up and you make a pull request, and then you build a preview of that. Maybe that's like a A staging environment that you might have a couple different variables for that. And then when you go to production, there's a whole new set of of environmental variables. So saying, hey. It looks like you accidentally Some magic
Wes Bos
environments, but it's awful in
Encrypted env vars are more secure
Scott Tolinski
a field that you're entering into
Scott Tolinski
and encrypting them into their database so that when you run the start scripts or whatever, it will automatically add those environment variables to your process dot node or your node process. So that way, you don't have to do anything other than implement them in their UI. You don't have to Load up an ENV file, and that's what we do for our sites even for for the the server or whatever. Our hosting platform has the encrypted values correctly, And then we only use the dotenv file locally. Yeah. That that's generally how it works with me as well. So I have a ENV file locally, My name is Scott.
Scott Tolinski
And that would be obfuscating
Transcript
Announcer
Monday. Monday. Monday.
Announcer
Open wide dev fans. Get ready to stuff your face with JavaScript, CSS, node modules, barbecue tips, get workflows, breakdancing,
.env files store env vars locally
Wes Bos
At both GitHub and Variables and then pull them down. Oh, that's nice. Yeah. I I wasn't using it because I just had a dotenv file, but I thought it was kinda cool. But You can obviously use them between staging and production Usually, when you're working locally, to set them. And then when you run your code, you access them. Usually, it depends on which language you're using, but every language out there uses these. They're available at process.env,
Wes Bos
in DigitalOcean's and Fig Figma is running
Scott Tolinski
is public. Now some people would say even in private repos, don't commit them. But we actually commit ours to our private repo Because our repo is going to remain private until the end of time, and it's not going to ever be public. Now there's probably a little bit of security concern there, but At the end of the day, I think we're we're okay
Wes Bos
All of your environmental variables. And then when you run your code, either your framework or via a package, They'll slurp up those environmental variables, and they'll be pulled into your application so you can access them like we we've probably talked about. Oh, we should mention too that That is explicitly
Wes Bos
and Google Maps API keys used to just be, like, whatever. You get a you get one, and everybody can You can see it, and it's not a big deal. And still, it's the Google Maps API key is still a client side variable, but the because I don't know Why they emailed me about this, but I had given it to
Scott Tolinski
something you should not do is Yes. Get that into your GitHub repo, especially if your GitHub repo Love it. Love it. Love it. So head on over to Sentry dot I o. Use the coupon code tasty treat, all lowercase, all one word, and you're gonna get 2 months for free of that sweet, sweet Sentry error and exception handling service. Cool. So e and v variables.
Scott Tolinski
Yeah. Abe, for the most part, keep these things out of your repo unless you explicitly know what you're doing. Yeah. Yeah. That that's a really, really good point. And even
Wes Bos
And then I noticed Vercel also has it now as well. So does render. Oh, render has it as well. It must be like a like a standard thing now across the board.
Wes Bos
like, Google Maps used to be, like, you get unlimited requests for free, and now it's, like, Six requests for a dollar or something like that. Yeah. Yeah. Yeah.
Scott Tolinski
with that being as the repo is you know, everything's locked down 2 FA and whatever. So,
Wes Bos
app platform,
What are environment variables?
Scott Tolinski
it's a world. Let's just say that. Yeah. Totally. So what are environmental variables?
Scott Tolinski
and Okay. This episode is sponsored by 2 of our favorite sponsors over here at Syntax, and that is Sanity and Sentry. Do you wanna Digging a little deep on on Sanity and I'll take Century, Wes. Yes. Sanity And let's say you have serverless functions, right, or or scheduled functions. They probably want to access things like maybe a Mongo URL with the Mongo secret. Well, you can create an environment group, and every single site that's a part of that group gets that environment variable rather than a specific one per service. And then you can override on per service basis. So you can have a group for all your things and then override them where needed. It's pretty nice. That's really, really nice. I like that a lot because I'll tell you an issue I ran into just on Friday.
Wes Bos
and this is specifically Node and JavaScript.
Scott Tolinski
Redone how we're doing all of our environment
Wes Bos
set on your machine or set via your host. Your host will usually give you some sort of UI Last 1 I have here is So if you wanna know,
Wes Bos
and they are often things that are secret. So these are variables that cannot go in your code Because they are sensitive in nature and or they change depending on the environment in which they're running in. So Things like API keys, even things like base URLs again, on the client side,
Scott Tolinski
variables ourselves,
Wes Bos
IDs for OAuth, or they have all kinds of different names, but generally, it's dotenv.
Scott Tolinski
E and v variables are one thing that can be a giant PIA, and you've been doing A little bit of a a deep dive and digging in here. So I'm interested to talk to you about some of your strategies and some of your ideas here because we we've recently
Today's sponsors are Sanity and Sentry
Wes Bos
make your own inputs and make your own customization. So It's kind of the best of both worlds because it's a hosted service. It comes with all this amazing stuff out of the box where you can have custom work flows and custom inputs and everything you could imagine. But then if you if you really want to, you can make your own inputs and make your own workflows and and whatnot. So, like, this really is the best of both worlds. So check it out, sanity.i0forward/syntax.
Wes Bos
on Sanity. That's pretty cool. Movember,
Wes Bos
it in your code. So what your CMS is can be kept in Git, And then you deploy this thing, and you can either host yourself or use Sanity to host it, the back end. This thing is called Sanity Studio,
Wes Bos
dot I o the ENV variables while you're at it. Yeah. It would. And there also is a one password In Netlify and Vercel, you can get the URL that is going to be built. So if you need stuff to be specific, if you need to put, like, a canonical URL or something like that, And you you have you can access via your environmental variable, and those things are are dynamic and available to you. The one, like, weird thing I found Even on my own Gatsby website is if you want those URLs, to do some stuff for my personal website, and I needed to put some variables in there. And they don't have any differentiation
Wes Bos
the marketing as well as the actual product. See the magic? Yeah. The magic is build beyond your expectation.
Wes Bos
Sanity is the ultimate content platform Conde Nast, Cloudflare. Basically, what it is is It's a a CMS. You configure Very, very important thing. So if you do want to expose them to the client because maybe they are client side API tokens, like, dash dash whatever argument to your node command. And the the reason why that's handy made SMTP credentials public.
Wes Bos
is the structured content CMS.
node_options sets CLI flags for node
Wes Bos
If you wanna use Chrome Dev Tools as your console instead of just the the terminal, That's that's a really, really good good way to put it. What else do we have here? There is also host provided variables.
Wes Bos
and that will spin up a little external In your client side, so in your React application, in your Vue application, in Next. Js, in Gatsby, And then you're only allowed
Scott Tolinski
Yeah. That is as somebody who has been really heavy into the ESM world, That is essential. A bit of knowledge right there.
Scott Tolinski
And I've used actually, I take this back. Yes. I have used it for more than just that. I've used it to increase
Wes Bos
this will hopefully not be a thing Npm install, make it work. Npm install, make it work. Yes.
Wes Bos
Commands that use Node under the hood. If you wanna be able to pass flags to them, the the most popular one is dash dash inspect.
Scott Tolinski
the amount of RAM available in node. Is that Like, memory or some? Yeah? From 1Password into a dotenv file. I see the wheels turning over there, Wes. I see these wheels turning. You you got into your your, what would it take to hook It's up to a thermal printer that could print out receipts for me as well.
Wes Bos
before you actually run your code, which is super handy.
Wes Bos
What that will do is it will require a package before it runs your your node script, AWS, which that's plenty if if you're able to decide which ones go where.
Wes Bos
Chrome DevTools for you. And the other one is if you use the ESM package, They always check change their tagline. I love it on their their home page. You can tell they're constantly tweaking
Scott Tolinski
Yeah. It's because specifically that was I don't know if I use it anymore. I'm trying to check through my code to see if I still have any of that. There was, like, a a neat little hack that allowed you to
Wes Bos
m. Dashresm.
Scott Tolinski
I've used the hyphen hyphen inspect,
Wes Bos
So if you just do node options database URLs, where your database is hosted,
Wes Bos
is because I had a dotenv dot sample just as, like, a sample of the values that we're gonna use in the course. And I commit that to GitHub because it's like a starter file. Right? Yeah. And immediately, I got an email from GitHub on So I thought that was that was kinda cool. So there kinda is different kinds of environmental variables. There's Plain text, which is just text, CLI tool. It would be so cool if there was, like, some sort of integration.
Scott Tolinski
Increase some of that memory for Meteor, and it made Meteor run a little bit smoother. Oh. And I don't see it in here. So you know? But I'm pretty sure that's how I did it. What I've used it for in the past is 2 things, is if you set the node options environmental variable before you run your node command,
Wes Bos
Sometimes you're not running, like, node index dot JS. Sometimes you're running and that will allow you to sort of insert
Multiple .env files can be useful
Scott Tolinski
dotenv I thought it was too until I just tried to load this next one. So the episode on Wednesday is going to be on building your own authentication system, which is something that I have been diving into Pretty significantly given that it's going to be a 2 part level up tutorials course for us. So the authentication, a lot of stuff there. So seeing you on Wednesday again, West, when I listen to this episode. Alright. Peace. Peace.
Scott Tolinski
Dotenv files. And to me, it just makes so much more sense
Scott Tolinski
the criticism against having multiple files. Like, that's like the famous GitHub issue in the
Scott Tolinski
So, like, if you wanted to set an environment variable named thing, it would just be thing equals yo Node index dot j s or whatever file you're running, and that sets that variable Yeah. On process dotenv dot thing. So, like, this is Probably not something that you'll want to do long term because you'll probably have a lot of these. Not to mention, this is the type of thing that would end up in your package dot JSON file, which Again, it could be harnessing secrets, which you probably would not want to do, but it's an option in case you wanna check things quickly or or do a quick test. Often, Mhmm.
Scott Tolinski
to have multiple files rather than to see, like, Development underscore Mongo URL. Right? You know, like Yeah. I don't know. That to me just doesn't make as much sense, but I I get it. There are conflicting or competing
Wes Bos
in
Scott Tolinski
Ideas here. Another thing I I do wanna mention too is that you can set environment variables
Scott Tolinski
npm package in GitHub. There's just, like, a lot of people
Scott Tolinski
having a conversation about whether or not it's appropriate to have different
Wes Bos
values depending on if you're building building it or if you're running in development mode. You know what? I don't Kent c Dodds called crossenv,
Scott Tolinski
Understand
Sentry overview
Scott Tolinski
And
Scott Tolinski
organized, just in your in running node scripts, especially if you wanna test Something you can just set them right before you run the node or node mod command.
Scott Tolinski
and when they last hit and if it was a regression and a GitHub issue attached to it and all sorts of amazing stuff that you want out of an error and exception handling service. It also tracks performance metrics as well, which is something that we definitely love and use. Using the latest, you can see exactly how fast all of your routes on your entire page are loading because as your users are hitting your site, It's sending some of that information to Sentry. It's almost like analytics for performance. It's fantastic.
Scott Tolinski
I absolutely could not exist without Sentry. Sentry is the tool that allows me to push with confidence when I know that if there is a major issue you going on on the site. I'm going to notice it very quickly because all of my errors and exceptions are logged, cataloged, on the client side. I agree to these terms and services of what you are warning me about.
Scott Tolinski
and even displayed blade in terms of the frequency
Storing env vars in 1Password is common
Wes Bos
package that's available in, like, PHP, in a Ruby, and and all that. The dotenv a dotenv file, and I actually did this when I This was intentional, but when I released my advanced React course, with serverless functions. You're only allowed 4,096 like, building and running in dev mode is if you set the node environment to production or development. Right? Mhmm. So you'll often see and then on next dev or keystone init or you're you're running, like, these different bytes, which is 4,096 Just regular
Wes Bos
Use multiple dotenv files. Like, only have a dotenv. Don't have, like, a dotenv dot local and a dotenv dot production.
Wes Bos
Pull that down in NPM scripts, somebody will say, like, node underscore env equals production, and then they run their their node command. And there's even A package from and it will pull in Yeah. That's how Snowpack works. Yeah. It'll it'll pull in those different node environment. All these things The machine that is running your code, And as soon as you do that, you must go immediately reset them. If you take it down, like, 12 seconds later, it's too late. There's bots that are are waiting for these types of things To accidentally go public. So you do that. And then another one I got on Friday was when I used to teach the boot camp. We would do Google Maps examples,
Scott Tolinski
Especially because our whole team has a a 1 Password team account, and we share passwords on it already. It's Everybody's got it set up and is logged into the team. Might as well hook them up with,
Wes Bos
and then It's the same thing as passing,
Wes Bos
I think that's that's about it for And its entire back end written in React. And if you want to, you can
Wes Bos
All the things. The dotenv a pull request to my blog, and it builds the website. Right? They don't have that, the differentiation between that and production.
Wes Bos
docs used to say, don't They've got Nike, Environmental Whatever it is, database underscore URL dot GitHub underscore API key dot
Scott Tolinski
your chin scratching mode. I was really seeing
node --inspect opens Chrome DevTools
Scott Tolinski
an episode
Scott Tolinski
how difficult some of this stuff can be. There are some best practices and whatnot, so hopefully you enjoyed that. Yeah. I hope so as well. It is one of those things that everybody's gonna deal with, and it's not one of the more fun aspects of everything. So, Well, that sounds good. I guess we
Scott Tolinski
will see you on Wednesday
Scott Tolinski
on if Notion ever loads.
Scott Tolinski
where we
Scott Tolinski
Bring to you
Hosts provide useful dynamic env vars
Wes Bos
they're not available on the client side. So you have to hot potato them in your Gatsby dash node or a or a dot env.local the only difference between,
Wes Bos
process .env.gatsby_commit that helps teams dream big and deliver quickly.
Wes Bos
What branch was used to trigger this build, or who was the person that made this commit? And so you can usually get all the Git stuff. You can get node options. Have you ever used the node underscore options environmental variable?
Wes Bos
It's equal to process dotenv.commit, but they're also are encrypted. And I I came across this you usually have To name them something special in order to surface them because you can't be going, a student. And 5 years later, the student put that project up on GitHub.
Wes Bos
your host will often give you variables of, like, what was the git commit that triggered this build or These are values that are and in all of your serverless functions, and we'll talk about front end ones in just a second as well. And then I went to Netlify
Wes Bos
and then you you explicitly prefix them with Gatsby underscore, and then there you're available to to get them.
Wes Bos
and say
Wes Bos
What about like, where do you keep this is actually a question we had earlier. It's like, where do you keep your environmental variables? And we didn't have a good answer. We got a lot of tweets, Which is all the same answer. I keep my keep mine in the private repo. This is where I keep mine. Uh-oh. Yeah. Yeah. So somebody asked, like, what do you do about, like, Like, where do you keep them secretly? And some someone, like, made a a script that would, like, put them in their own folder. A lot of people just said, just put them in 1 password, and I was like, oh, yeah. Obviously. That's a great idea. Yeah.
Scott introduces himself and Wes Bos
Scott Tolinski
Variables.
Scott Tolinski
Hey, Streat. We're gonna be talking all about our environment,
Scott Tolinski
so to say. My name is Scott Talinski. I'm a full stack developer from Denver, Colorado. And with me, as always, is Wes Bosch.
Scott Tolinski
Welcome to Syntax in this Monday.
Scott Tolinski
That sticks along with the different aspects of how your application is running, different environments,
Scott Tolinski
These are the variables that you use that are often kept as secrets or maybe they're just some of the variable information the the actual UI of your hosting platform,
Sanity overview and details
Scott Tolinski
We love Sentry over here at LevelUp Tutorials and Syntax. That's what I use it on my platform, LevelUp Tutorials.
Wes Bos
It's gonna get you double the free usage tier. And tweet us what you're making with Sanity. We've had a couple people do that lately, and it's kinda cool to see what people have built on it. Sick. This episode is also sponsored by Sentry, and that's at Sentry dot I o. Now Sentry is the perfect place to track all of your errors and exceptions.
Env var groups help organize across services
Wes Bos
on AWS public ones On the front end, you have to prefix them with, and I'll give a couple of examples. Create React app is you have to prefix them with React underscore app. Gatsby, you have to prefix them with Gatsby underscore next. You have to prefix them with next underscore. In that way, you are explicitly typing it, and you say, yes. I I understand that this needs to be available
Wes Bos
Characters.
Wes Bos
for building my website. These are variables for the serverless functions. It's all just all in 1, and I hit that limit. And I had to just go back and rename them to be very much shorter variable names, which is a pain in the butt. But, like, that kinda is the limitation of Netlify, but, also, it's it's a hard limit imposed on serverless functions from environmental variables. And,
Wes Bos
f. Between, like, a preview URL, like, someone submits
Wes Bos
regular characters. Some emojis and things like that are multiple bytes, but just regular characters are. And I ran into an issue because, like, there I just There's no way to say, okay. These are variables for staging. These are variables for production. These are variables
Wes Bos
I was deploying to Vercel, like, publishable keys like Stripe, all the environments in your npm scripts, which is super nice.
Wes Bos
characters if you're using Process.env.
Wes Bos
So I had to duplicate a bunch of variables and just tag on underscore staging variables are variables that are set I got an email from Google within 12 seconds of them pushing that commit, which blew my mind. And I was able to log in to my Google s and invalidate that key just because now those keys are
Wes Bos
and Vercel allows you to have production You wanna explain what they are? So it's basically
Wes Bos
onto the end of them. And then in my code, I had to, like, check if we were in production or not. And usually, the way that you do that is with process s.env.node_env.
Wes Bos
environment variables, Staging environmental variables, I think that's about it. That's surprisingly a lot to talk about environmental variables. Like, I, like, had this idea. I was like, we should do a a show on environmental variables. And I was like, ah, that's So simple. Everybody knows that. And then I went through a day of just smashing my head on the desk with Windows. So cross env will And then what we hit in In production is a just whatever .com. There's no staging on it. So I used to check if we were in the right environment and then and then set a variable to that. And then I I just took it out and said, you know, I'm sticking the whole URL directly in In Next. Js, you have a dotenv.local.env.developmentand.env.production,
Today's podcast will cover environmental variables
Announcer
soft skill, web development, the hastiest, the craziest, the tastiest TS, web development treats coming in hot. Here is Wes, Barracuda,
Announcer
Boss, and Scott, El Toro Loco,
Announcer
Tolinski.
Prefix client-side env vars differently
Scott Tolinski
on your React application because that would be a massive security flaw. And if those somehow got into memory or to a config and someone could find them, then you'd be you'd be in trouble. So I think the key thing to emphasize is that anything you send to the client, no matter what, can get gotten Yes. By somebody. So if you don't wanna get got, you got to not Expose any of those secrets on the client whatsoever.
Wes Bos
is a good example. On Stripe, you get a secret key, Which goes on your server, and you get a public key which goes in your HTML.
Wes Bos
Mhmm. What else? If you want environmental variables
Wes Bos
And if you want to access those
Wes Bos
say, process .env.databaseURL
Env vars are not checked into source control
Wes Bos
For an API, I'm just working on it. I want an API right now, and the API that we hit in development is staging.whatever.com.
Wes Bos
you'll have a file that's called the dotenv And I I ran out I ran out of space. Very specific number. Yeah. Yeah. Yeah. Yeah. Yeah. And, like, It's 4 bytes. That's or 4 no. Sorry. 4,096
Wes Bos
an environmental variable. Mhmm. So let's talk about different types And this is just a key value file where you stick Mix and match your common JS and your your ESM.
Wes Bos
of environmental variables and and how they can be set. So at the very basic, you have plain text, Google, if you accidentally commit But I, many times, have have had multiples just so that I can keep them in a file somewhere so I can reference them. Also,
Wes jokingly calls himself the "variable master"
Scott Tolinski
Variable master disaster
Scott Tolinski
getting it faster. Blaster. Okay. Oh, that's good. Yeah. Oh, yeah.
Use both plain text and encrypted env vars
Scott Tolinski
so you can specify a group of environment variables Yeah. For an entire, like, service.
Scott Tolinski
staging URLs and callback URLs, things like that, and then encrypted for things like database or OAuth secrets, things like that. You know what that render has? I don't know if other platforms have it because render is the only one I've seen it. They have environment groups,
Wes Bos
and they can only be
Wes Bos
The server, you have your inputs. Like, the encrypting part is pretty cool. I hadn't seen that before. And the idea behind that is that If somebody does get access to the DigitalOcean database of environmental variables Mhmm. They're encrypted,
Next show will cover building authentication
Scott Tolinski
Head on over to CSF. Use syntax.fm for a full archive of all of our shows. And don't forget to subscribe in your podcast player or drop a review if you like this show.
Scott Tolinski
Notion.
Wes Bos
Okay. Whatever. You're not getting felt like it was a bit faster today? in a couple years. But the ESM package will allow you to The variable master, They call me in high school. The variable master.