12 Days of Baddass Courses sale! Get instant access to the entire egghead library of courses and lessons for 58% off.

Runs out in:
05 : 09 : 33 : 09
Join egghead, unlock knowledge.

Want more egghead? It's 58% off for a limited time only!

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock All Content for 40% Off
Become a member
to unlock all features

    CommonJS Basics Introduction

    Ben ClinkinbeardBen Clinkinbeard

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



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson




    Man 1: 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.