CommonJS Basics Introduction

Share this video with your friends

Social Share Links

Send Tweet
Published 10 years ago
Updated 5 years ago

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

Man 1: [00:01] 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.

[00:10] 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.

[00:47] 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.

[01:17] 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.

[02:05] 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.

[02:36] 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.

[03:10] If we then run this again, you can see we get the same output and everything works the same.

egghead
egghead
~ 48 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today