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

Scott introduces what Node modules are, and recommends ESM as it is the newest standard. This course will be using CommonJS.

Get Unlimited Access Now

Transcript from the "Node.js Modules" Lesson

[00:00:00]
>> So now, what we're gonna do is we're gonna talk about modules. So what are modules? Anybody wanna tell me what are modules and their definition? I can't call you again, Scott Fox. I gotta save it, David.
>> It's a package bit of JavaScript that executes on its own.

[00:00:20] Well, not necessarily on it's own but a package of JavaScript
>> It's a package of JavaScript, that is right, anybody else? Andy, what do you think? I do like you think at heart. More like you know what the module is but you just don't want to give them my answers.

[00:00:36] What do you think, Seth?
>> I go up in a limb and say, encapsulated code because that's utility R was.
>> Yeah, [LAUGH] That is exactly right, it's encapsulated code or package JavaScript. You can think of it like it's safe, it's safe from all the other code that's trying to mess with it, right?

[00:00:52] Is its own thing and you have to explicitly ask for it. And what you get back is only what it said, you can get back, right? It's very stingy with its code, it's very stingy with itself, like here is everything about me, but I'm only gonna let you know about this.

[00:01:07] And this is how you interact with me, that's a module, and you can't affect it anyway else. This is why you should use globals because, if your module is using a global and you have another module using a global. And they both use the same property, now, you just news messed everything up, right?

[00:01:21] You had this one global that you changed and now, it affected that all this other code at another module. And now, you don't really have modules anymore, right? You have all these side effects, so yeah, modules is like encapsulated code and you might have written modules already in JavaScript.

[00:01:35] And you probably didn't even know it, that's because they kind of look like this. So basically, each one of these functions here is two different files. You have module one and it's basically just like an IIFE, right? So you have a function that's encapsulated and then, something, somewhere, some magical thing is gonna inject in all these globals.

[00:01:57] Here's all these globals. Here you go, you can have those. And then, all the code that you write is just gonna be here. That's it, right? And then, you might have another module that looks exactly the same. It's gonna have some function and it's gonna be like some global setter injected into it and then all the code that you write is here.

[00:02:13] And if you wanna use another module, you would have to ask for it. And then, this runtime, this environment would go find the module that you're asking for by name and give it to you. And then, only give to you what the module that you're asking for has allowed you to have and have to export those certain things.

[00:02:30] So basically, module isn't encapsulated code. So by putting parentheses around this and they're invoking it later, this is encapsulated, nothing can touch it and you probably do this a lot. People did this a lot in like 2012, 2011, right? They just put everything in iffy, so it's like encapsulated.

[00:02:46] If you've ever written like Anger one back in the day, this is what you did. If you've ever written Backbone this is what you did, like to encapsulate all your code. This is why they did it, they were like, everything's on a window, it's gross, we need like modules.

[00:02:59] So we're gonna like wrap everything in a IIFE, which stands for Immediately-Invoked Function. Yeah, so it's like you basically create a function and you encapsulate with this parenthesis and you invoke it immediately, so it's safe from everything. So that's what people did and some people literally have built to as it would go wrap every file in IIFE.

[00:03:20] Cuz it was such a big deal and because it was such a big deal, what's' a lot of people decided to do is like module systems. So that's where CommonJS was born, that's where AMD was born. And now, we have ES6 Modules. So these are all different module systems that solve the problem with people having to do this.

[00:03:37] Which just looks gross, so I was like we're just gonna make a module of system that loads things up asynchronously or synchronously in the browser and do it that way, that way you can just do it. And they are pretty legit but they don't work quite as well as they do a node.

[00:03:52] So that's basically what a module is. Any questions on modules, yes?
>> So I suppose that the globals are available everywhere in node JS, then why did you have to pause them?
>> You don't. You don't, okay, yeah, I'm sorry. This is an example of you more to like some type of module thing or so.

[00:04:10] You won't have to do this. Let me show you. Check this out. Let me see, let me throw an error somewhere. So if I go back into my code and I just say, throw new Error ('opps), right? And I run this, wrong command. There we go. Node code literally won't let it.

[00:04:37] You see this right here, I didn't write that, how did I get there? Node code put it out there for me. It did what I said most people would do. They'll create a bill tool and wrap their code in this IIFE, node just did that for you. That its job.

[00:04:51] Its job is to take every file that you write, wrap it into something like this and then, boom you get all these globals. There's exports, there's required, there's module file name, dirname. You get all these stuff. That's how your code is able to use those globals because that's what Node does.

[00:05:05] Yes, Taylor?
>> Why are these five globals present in as parameters of the IIFE whereas, there's 20 globals that I'm seeing on the documentation?
>> Yeah, so what's actually happening is they have the global global, right? Remember, the global global I talked about. So the ones that you don't see are attached to the global global and that's because those.

[00:05:30]
>> You set timeout or whatever?
>> Right, set time out. Those are attached to the global global, and that's fine because there is only one instance of those. The ones that are here, these are this way because there's a new instance of them every time, right? They're a different value, so they have to inject them in every single time according to your file.

[00:05:47] For instance, dirname is different depending on what directory you're in. Filename is different depending on what file you're in. Modules different, requires different, exports. All of these things are different depending on what file you're in. So they have to inject those every single time. Whereas, all the other one never changed.

[00:06:00] So this attached on the global and then you have them. Does that make sense?
>> Cool. Cool and then that's pretty cool, I mean that literally shows you. I mean that's legit, it didn't used to be that way, I mean they hooked this thing up. [LAUGH] It's really good.

[00:06:17] But yeah, you won't need to write that, that's free. So if you ever see someone writing code and node and then they are doing this. Just don't approve their PR, like add a message and be like no, like no one does already, like no what are you doing?

[00:06:30] You only need to make another module in a module. You don't need to, it's free. Any questions on that? No, okay, modules in Node, like I said. Tdlr, CommonJS. You can look up CommonJS. It's CommonJS 2.0 at this point. But you don't really need to. It's pretty simple, I'm gonna talk about it.

[00:06:50] It's super simple, you don't really need to look it up. But if you wanna be an expert on CommonJS, go look it up but honestly you shouldn't because Nodes gonna move over ES6 module anyway. And were already doing it, is just super pain to do it right now, but we'll talk about it.

[00:07:04] So like said NodeJS uses commonjS for its module system. And again, its module system is required there is no other way to use another file and another file without modules and Nodejs. There's no script tags, there's no DOMs, so you have this module series absolutely no other way.

[00:07:19] It's the only way to share a code. And like I said, there are other modules out there like ESM which is ecmascript modules, and that's the new standard. The browsers that are using modules today use ecmascript modules. Actually, I use ecmascript modules in node but I have to use what's called a transpiler.

[00:07:38] To transpile them down to something that node can understand. So I use something like Babble or wet pack to take code that isn't available yet in node and that tool will make it work in node, right? It's compiling a coffee scripts like you've got this thing that the browser doesn't understand, so you have to build it to something the browser can understand.

[00:07:57] So I use a transpiler for that. So Babel, Typescript is a really other good subset if you want typing. There's a lot of them out there. But I would use something like that, so that way I could use ES6 modules today without having to wait for the node or to figure out this mess with commonJs and the ES6 modules.

[00:08:13] Cuz it is a mess and I'm just not gonna wait for it. So ESM, ecmascript modules is one. AMD, I shouldn't even put this here, cuz I don't want anybody looking this up. But there's also another one, [LAUGH] but please, do not ever use AMD, okay? AMD is kind of confusing, because the naming is like AMD, Asynchronous Module Definition, I think is what it stands for.

[00:08:36] But they use one of the frameworks that were built on top of AMD it's called RequireJS. So a lot of people get confused with RequireJS and CommonJS because CommonJS uses a require function. So when people hear RequireJS, they think, that's Node, like no it's not, node uses a function called Require, but that's CommonJS.

[00:08:54] RequireJS is something completely different than CommonJs, and I highly not recommend using, it's dead technology. If you wanna see what happened to the web and how they got here, you can go look that up but don't ever use it, just stay away from it. I think the only people I know that actively goes out and uses AMD is Google.

[00:09:15] But they do something really awesome where they do like optimization, very, very good optimization with AMD. But it's a built tool they don't hand write it, it's like some inline thing, very clever use of it, but you don't ever need to use AMD ever, just don't use it.

[00:09:29] I remember using it back in like, 2013 or 14, it was the biggest pain in my life, just so bad. But whoever made it, thank you for making it. It was a great back then. But now, yeah, don't do it. [LAUGH] Others, there are others, tons of different types of other module things out there, they just don't matter.

[00:09:51] You can just get lost on Google for different types of module loaders out there. They literally do not matter. Because if you introduce a module system today in your app that pretty much isn't ecmascript like, it's gonna die tomorrow. Cuz it's a standard now. Ecma-made modules are standard, so people that don't use that standard, so anything that's not that is pretty much gonna die.

[00:10:09] I mean, even commonJS is dying and node is like, how do we fix this? So just don't do anything else, just use ecmascript. If you don't want to use a transpiler, just use Node and commonJs and wait for the change, that's it, do not do anything else. Don't go try to make one, just don't do it.

[00:10:24] Just use what's there and be done with it. You don't have to, nobody wants to make a module system, that's just not fun, trust me.