From the course: Node.js Essential Training

Unlock the full course today

Join today to access over 24,800 courses taught by industry experts.

Exporting custom modules

Exporting custom modules - Node.js Tutorial

From the course: Node.js Essential Training

Exporting custom modules

- [Instructor] Every JavaScript file is a module. We've been loading modules with the require function, and the require function is part of that common JS module pattern, but it only represents half of the pattern. The half that loads the modules. The other half of the pattern is module.exports or the mechanism by which we make our modules consumable. The first thing I'll do here in 03_04's start is I'm going to create a file called myModule.js, and in it we're going to create a few variables. So we're going to create a count variable. We'll set that equal to zero and a few different functions. So the first we'll be for incrementing, and this is going to increment our count, add one to our count, and then we'll also create a decrement function that is going to do the opposite. So --count will remove one, then we'll create this getter function that's going to get whatever the most recent count is. Perfect, so now what I can…

Contents