This course has been updated! We now recommend you take the Introduction to Node.js, v3 course.

Check out a free preview of the full Introduction to Node.js, v2 course:
The "Globals" Lesson is part of the full, Introduction to Node.js, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Scott walks through the global properties available in the Node.js environment. The most common global properties are __dirname, __filename, and process. The exports, module, and require properties are used for creating and exposing modules across the app.

Get Unlimited Access Now

Transcript from the "Globals" Lesson

>> Let's kind of talk about the environment of Node. I scratched the surface about some of the differences in the browser versus Node.js, but I really wanna dig in a little deeper here. And talk about some of the globals that we get access to. Which are gonna be super helpful for us when we're developing our apps inside of Node.js.

[00:00:16] So like the browser, Node.js does come with some practical globals for us in our applications. Not as many. If you go to the browser, anyone ever did window, and then hit dot and just go through this list? That scrollbar's barely moving. There's a lot of stuff on the window.

[00:00:36] So they're not gonna have this much stuff on the global in Node. But yeah, there are some globals in there that we will be talking about. So some of the ones that I wanted to mention was literally this thing called global. So if I go to Node and I say console.log(global), I mean, it's literally called global.

[00:00:56] And I execute this in the terminal, we get this strange looking object with some crazy stuff on it. This is just what the terminal, or this is what Node is representing it as we log it. But at the end of the day, it's just an object with some familiar properties on them.

[00:01:11] You've probably heard of clearInterval and clearTimeout, setTimeout, setInterval. Those exists in the browser. And they work pretty much the same way here as well. Then you might see some other stuff like queueMicrotask and setImmediate, and things like that. And that has to do with kinda how the event loop works in Node.js.

[00:01:31] Which is something that we will talk about a little bit. But we won't really go into detail about the event loop. Because I don't think it's that important when you're learning Node.js. I do think it is important when you're trying to get the best out of Node.js, though.

[00:01:45] And you can actually look at other courses we have on Frontend Masters where we talk about that. So, yeah, you have something called global, here. That's got some really nice things. And then if we go back to our documentation website, there's this other thing called __dirname, and then the equivalent filename.

[00:02:03] And these two globals are very helpful because they give you the current directory name or the current file name that you're executing in. So what does that actually mean? Well, if I go back to my code here and I console.log(__dirname, _filename), and I run these, you can see for the dirname, it's literally the path to the folder that I'm in.

[00:02:26] It's the directory name relative to the file that got executed. And then a filename is the file that you wrote that variable in. So this is very helpful. Because as you'll see, in Node.js, or any OS level-based language, you're gonna be interacting with files quite often. Whether it's you're uploading an image.

[00:02:46] Whether writing to a configuration file. Or reading a configuration file, or environment variables, there's just so many times you're interacting with files, especially if you're building a build tool like Webpack. You're literally only interacting with files. So getting these paths right is gonna be crucial to you being able to develop those.

[00:03:03] So these two globals are extremely helpful. We have something called process, which I probably ague is the most helpful thing inside of Node.js. And this is when you start to understand the power of Node.js compared to a browser. Which is also powerful in it own right, but you can see just how different they are.

[00:03:24] So we go to handy log over here and we check out the process object and we log this, I hope I'm not going to wait, do I wanna log this? I wonder if it's going to show anything that I don't wanna show. I don't know. Let's see, there's a lot of stuff on the process, okay.

[00:03:41] So the process, it's the object where everything about the environment is stored. So you'll see what platform, you're on the version of Node, different internal things. One of the most important things that you'll see is gonna be environment variables, which is somewhere on here. Here we go. So that's what I didn't wanna show, is those environment rebels.

[00:04:04] So you gonna see global environment variables that are exposed on your computer. Which is super helpful when you're deploying a server or something like that. Because you don't wanna have your secrets out in your GitHub repo and really tick someone off at your company. You wanna put those somewhere in an environment and have them be injected in.

[00:04:22] And you access those environment variables through the process. So if you've ever like written some React thing somewhere and they had you process.env.something, and you really know what that was, that's what this is. You're reading the process from the environment that the built tool that's processing your React is running in.

[00:04:41] So, yeah, that was pretty meta. But yes, your build tools run in Node and they process your frontend before the browser sees them. And because they are in Node, they have access to the process, which gives them access to the environment variables. And that's how you're able to embed different environment variables inside of a frontend app before it gets deployed to a website.

[00:05:01] That's it, so a lot of cool stuff here on the process. And we'll be talking a little more about that later. And then you have these things with modules. And we're gonna get some modules, because right now modules are in a exciting phase in Node.js. And we're gonna be using the latest version of the modules.

[00:05:23] But I do wanna talk about the few here. So we have exports, we have module, and we have require. And these are globals that are gonna used for, I would say, the old module syntax inside of Node.js. And like I said, we're gonna talk about it more. But it's nice to know what these are.

[00:05:41] And we're gonna dive in. But yeah, you're gonna see this a lot in Node.js. They get injected into the environment for you. You don't have to worry about importing them or anything like that. They're just there available. And they're used so you can create your own modules and expose them and share them.

[00:05:57] And deploy them, publish them, whatever you want. So, yes, we're gonna check those out as well. And then like I said, there's really just a lot of stuff in the global space that we didn't talk about. I put a link here to the documentation website that goes over all the stuff.

[00:06:13] Because, just like the browser, you don't need to know everything that's on the window. Just like you don't need to know all of this stuff. You're probably won't even use half of it to be honest. Especially if you're using the new module syntax, you won't even use a lot of this stuff.

[00:06:26] And you most likely will be using packages that you install, which we also are going to cover. Any questions on globals and global objects and variables inside of the Node.js runtime?