- [Instructor] In Node.js, every JavaScript file
is its own module.
Now, we've been loading modules with the require function.
The require function is part of the common
JS module pattern, but it only represents half the pattern,
the half that loads the modules.
The other half of the pattern is module.exports,
or the mechanism that we use to export data
and functionality from a module.
Let's take a look.
Let's create a new sample file
within our files called myModule.js.
And from myModule.js, I can call module.exports
to export any JavaScript type or values.
So I'll go ahead and export Alex.
And then from another file, I can consume this file
with the require function.
So if I go into my files, I can create a new file
called app.js, and we can consume the value
that I exported from myModule.
With the require function, all we have to do
is specify the path to the module.
So I'm going to start with a ./myModule.
Now, we don't have to provide the path
when we import core Node.js modules
or when we import modules that we've installed with NPM.
But whenever we want to load other JavaScript files
that we've created within our file system,
we have to provide the path to that module.
Let's go ahead and log the name,
and I will open up the terminal,
and we can run our app.
And when we do so, we see that we have exported Alex.
The require function that we've invoked on line one
will export the value of module.exports from our other file.
Let's go ahead and make more of a tiny example of this.
I'm going to go ahead and create a variable called count,
and we'll set that equal to zero.
And then I'll create a function for incrementing the count.
So we'll say plus plus count.
And we'll make a function for decrementing the count.
So we'll say minus minus count.
Now, within every JavaScript file, all of the variables
are scoped to that module.
So the files that consume this module
won't have access to the count.
What we're going to do is create a function
for returning the value of the count.
getCount will return the current value of our count.
Great, and now that I have all three of these functions,
I can export them together in an object.
module.exports allows us to export any JavaScript type,
so we'll just export an object by using the curly brackets
and then we will also, whoops,
let me go ahead and collapse the terminal for a minute
so we can see all the code at once.
So I'm going to export the increment function,
the decrement function, and the getCount.
There we go.
Now I can save this.
And then go into my app.js file.
So we're not going to call the variable name anymore.
We're going to call it counter.
And I could use those functions that I created
in a separate file, counter dot increment,
counter dot increment, counter dot increment,
and then we can come down here
to our console.log and do counter.getCount.
Let's go ahead and save this.
And then open up our terminal
and run our app by typing node app,
and we can see that we've incremented the count
three times and we are able to use the getCount function
to return that variable, because the count itself
is scoped to myModule.
Now something else we can do just to make this file
a little nicer if we wanted,
is we can destructure these functions when we invoke
the require function.
So instead of setting 'em all to an object called counter,
I can just destructure what I need.
In this case, we need the increment and the getCount.
And we'll also get the decrement and use that once too.
So now, I don't need to preface everything with the counter.
We've just imported the increment function
and the decrement function
and the getCount function directly.
We'll do one decrement.
So now we can save this, go to the terminal,
and run our app again.
And we can see that using that decrement function once
changes the count from three to two.
So the Node.js module system allows us to separate
our functionality into separate files.
We consume that functionality with the require function
and what gets returned by the require function
is what we've exported with the module.exports function.
So here I'm looking at myModule.js.
And notice that the increment, decrement
and getCount function are being exported here
and they are imported with the require function
to our app.js file.