Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 986 of the free egghead.io lessons, plus get Node.js content delivered directly to your inbox!



Existing egghead members will not see this. Sign in.

CommonJS Basics Introduction

3:18 Node.js lesson by

CoomonJS modules provide a clean syntax for importing dependencies. This lesson will take a look at the basics of using CommonJS modules.


egghead.io comment guidelines

Avatar
egghead.io

CoomonJS modules provide a clean syntax for importing dependencies. This lesson will take a look at the basics of using CommonJS modules.

In the CommonJS module format, the way that you expose a value to other files that want to reference your file is by using the module.exports object.

In this case, you can see we're simply assigning a string to module.exports that says I am a dep, and then if we go over here to main.js you can see that we're then pulling in the value of that file using the require statement. So, require./dep without a file extension will pull in the value that is exposed by that module.exports statement. Then you can see here that we're just going to log that out. If we then just say node main.js, you can see we get I am a dep logged out to the console.

You can expose any sort of value. We could expose a function here that we'll just have it return the same string. Fix our spacing here. If we then go back to main.js, we are now going to be getting that function reference back in this variable. We're actually going to have to call that function if we want to get the same output. There you go.

This is the main way that you're going to see things exposed from CommonJS modules. Generally it's going to be a function. Not always, but usually modules will expose a function. Generally they're going to expose a single value. Now, if you want to expose multiple values, you could do it just by exposing an object here. If we just say foo=foo and bar=bar, go back to main.js, and we can then say dep.foo and dep.bar, run that again, and you can see we've got foo and bar there.

Now, that works, and it's valid from a technical standpoint, but it's not idiomatic. In node.js or browser, CommonJS in general, if you want to expose multiple values you should really use the exports object directly. What that means is that the module object here that we're referencing is provided by the node environment natively or by browser, if you're using it in that context.

There is also an exports object that's provided that is actually a reference to this exports objects on the module object, but you can reference it directly. The way that you would do this idiomatically is like this. If you want to expose multiple values, you should generally do it this way where you assign to exports directly. That's going to be what the code that you come across is going to look like.

If we then run this again, you can see we get the same output and everything works the same.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?