Check out a free preview of the full Digging Into Node.js course

The "Environment Variables" Lesson is part of the full, Digging Into Node.js course featured in this preview video. Here's what you'd learn in this lesson:

Kyle introduces environment variables for the shell with a few of their use cases, and configures the program to receive and utilize environment variables. The path.join method is introduced.

Preview
Close

Transcript from the "Environment Variables" Lesson

[00:00:00]
>> Kyle Simpson: Pulling in inputs from standard end or from the parameters, that's pretty good. But we also can get inputs into our program in one other way which is through the use of environment variables. This is again one of those ones that may change a little bit depending on if you're using a Linux or Mac system versus a Windows system.

[00:00:19]
But in Linux or Mac, if you want to set an environment variable, it's kind of like setting a global variable. It's a variable that would be automatically available to a program. But if you wanna do it on a per command basis, you basically just prefix something. So if i said HELLO=WORLD, and then I said ./ex1.js,

[00:00:44]
>> Kyle Simpson: That would set an environment variable for my shell environment called HELLO with the value WORLD, but only for this command. And then it goes away. So it's like creating an environment. Now these are typically used to configure a development versus a production environment or paths or things like that.

[00:01:04]
But there's a lot of different ways that you might wanna take a program and being able to configure it through the usage of environment variables. So what we're gonna do if I run that, nothing, it doesn't see that but we're gonna make our program be able to see and use that information.

[00:01:20]
So what I'm going to do is I'll do it here.
>> Kyle Simpson: I'm gonna say if process.env. And then the properties on this one will be the environment variables that have been set including the one that we just set. So if there's a process.env.HELLO, we wanna go ahead and print that out.

[00:01:53]
>> Kyle Simpson: Let's see what happens now. You'll notice that it did in fact find and print out our environment variable there.
>> Kyle Simpson: So we can use this for, in this particular case, we're gonna use it for configuring a base file path. Okay, so I'm gonna say that our BASE_PATH.

[00:02:24]
And we want a default base path to underscore underscore dirname unless this process environment has been set, in which case we'll use it. So we're gonna say,
>> Kyle Simpson: We're gonna use path.resolve, and we're gonna use either,
>> Kyle Simpson: The process.env.BASE_PATH or if that one hasn't been set, then we're just gonna use _ _ dirname.

[00:03:16]
>> Kyle Simpson: Armed with that information, we're gonna change our usage of path.resolve here. So instead of saying path.resolve, we're gonna use a different one that's called path.join. path.join will take any number of inputs, and use the appropriate directory separator for your platform. So on Linux or Mac, it will use the forward slash, on Windows, it'll use the backslash.

[00:03:40]
It'll do any other kind of escaping it needs to do. So here, we can just say, path.join(BASE_PATH) with our args.file.
>> Kyle Simpson: That'll allow us to assume dirname unless we override it with an environment variable, and that tells us to find it from somewhere else.
>> Kyle Simpson: So let's use BASE_PATH.

[00:04:11]
>> Kyle Simpson: And let's say files/ because that's the one we've already been grabbing stuff from. And now let's use --file=, and we can just say hello.txt. Because it's going to know to look for it in this configured base path.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now