Check out a free preview of the full API Design in Node.js (using Express & Mongo) course:
The "Node Environment Variables" Lesson is part of the full, API Design in Node.js (using Express & Mongo) course featured in this preview video. Here's what you'd learn in this lesson:

Scott spends a couple minutes talking about Node environment variables. All environment variables are stored on a global process.env variable. One important variable is the process.env.NODE_ENV which determines if an application is running in a production or test environment.

Get Unlimited Access Now

Transcript from the "Node Environment Variables" Lesson

[00:00:00]
>> [MUSIC]

[00:00:03]
>> Speaker 1: So small little note, now that we're talking about tests it's worth mentioning. God. Suspect typo. Mentioning envarionment variables. Environment variables, sorry. I said envarionment variables, I was reading the typo. [LAUGH] Environment variables. And how we can use them. There exists a process.env object globally in node. So I'll show you what that looks like.

[00:00:26] So again, going back to how node handles files. I'm just going to comment this stuff real quick so you can see. So again, there's an iife around this entire file. Everybody knows what I say when I say iife? Immediately invoke functional expression. So there's a iife here that's creating a closure.

[00:00:48] And again, we get console, we get all this other stuff, right, and there is also process, that's another one, there's so many args up there. So process is the actual process that you're running in. So what we can do is we can look at that process and I'm just show you what it looks like.

[00:01:10]
>> Speaker 1: Let's do that node index here's the process object. So all this stuff, a whole bunch of stuff. And this is everything about the environment that we're running in on my current machine. This is the process that this node is running and when it executed this file. So there exists a process.env, which is short for environment.

[00:01:36] And this is the stuff. Let me clear the console, it's so much noise. Here we go. Here's the process.env. And you can think of this like the object that's gonna hold all of our environment variables. So here's all my environment variables. All these secrets and stuff on here that I shouldn't be showing.

[00:01:51] [LAUGH] So that's our environment. And this is where we're gonna be telling node about hey, you're in production or you're in testing or you're in development. And here's the secret for your application. Here's the Facebook secret for. That's how we're gonna pass in those environments. And every application we deploy too allows us to do that using environment variables.

[00:02:12] Every environment variable that we pass in binds to process.env.
>> Speaker 1: All right, so if I were to say.
>> Speaker 1: Let's say, node index.js, or I'm sorry hold on, export hey equals hey. And then node index.js. [SOUND] Did it do it?
>> Speaker 2: Mm-hm, it's at the bottom.
>> Speaker 1: It's at the bottom?

[00:02:47] Yeah there it is. So there it is, hey. So I made environment variable, put it there.
>> Speaker 1: See that, so that's how we do environment variables. Any questions on that?
>> Speaker 2: Go back to that line.
>> Speaker 1: Which line? This one?
>> Speaker 2: Before this. Where you actually did that command line.

[00:03:08]
>> Speaker 1: The command that I did? That
>> Speaker 1: So yeah, if I say export and do like this assignment that's creating environment variable then put this colon here like I'm gonna run another command on the same line.
>> Speaker 1: So that's an object of environment variables, then that also means you can set environment variables too.

[00:03:39] So, process.env.whatever equals whatever you want. And that will also set environment variables. So it's two-way.
>> Speaker 1: Cool, so yeah, the process.env object is globally in node. And the one useful property on that object is the one that's called NODE_ENV. So the convention for environment variables, and I'm sure this is just not a node but just in general, it's to capitalize them all.

[00:04:12] Like all caps and underscore or I guess it's technically not camel case, but yeah. Separate by underscore. That's the convention is to have your environment variables look like that. But this important one is NODE_ENV, and this is what we use as node developers to determine what environment we're in and its value can equal testing, development, production, something like that.

[00:04:37] But that's like the convention. Mark, you had a question?
>> Speaker 2: Yeah, do you recommend declaring environmental variables in a start script, like upstart or putting them in a separate ENV file?
>> Speaker 1: So that all depends on how sensitive those variables are if they're like secret or tokens, you definitely don't want to do that because then they'll be checked into source control.

[00:04:58] And then once they GitHub, even if you check them back out they're there forever. Because GitHub does hooks. So somebody has built a server that listens to every single hook on GitHub. So it doesn't matter if you put it on there and get rid of it. It's there.

[00:05:08] So, do not put secrets in the file. If they're not sensitive, a start script is fine. A separate file is great too. There are node modules. There's one actually called ENV that kinda does this for you. If you have a build system incorporating it with gulp is actually really cool too.

[00:05:23] So there's a lot of ways you can do it. And then it also depends on like, what environment your application is gonna run in. So if you're talking about deployment, then what you would probably do is the service that you're using has like, probably gooey where you can just type in your environment variables.

[00:05:39] Right there inside the actual admin panel of your deployment. I know AWS allows you to do this, Heroku allows you to do this. So you don't really need that file. But locally, yeah, you'll probably need that file, but we're gonna talk about how to get around checking that into source control when we get into the production part tomorrow.