Node Modules Basics
If you recall from the Intro to Node post, one of the main requirements of having JavaScript be able to work on a server is to have code that can be more easily organized. Thus far in JavaScript, we're used to having each JavaScript file be completely on its own, where it can't rely on any code written in any other JavaScript file. (AngularJS is excluded from this, because it has its own solution to allow referencing code written in other files - dependency injection).
The way Node.js deals with allowing for code separation is through modularization, or creating separated modules.
Note: the specific syntax for how this is done in JavaScript is about to change in ES2015 (A.K.A. ECMAScript 6), but the principles will be very similar. Also, because adoption of language updates is so slow, you will see this method of modularization for quite a while longer.
require()
By this point in the course you've likely used the global require()
function in some of your JavaScript exercises. For example, in the JavaScript Calculator exercise, you had to install the readline-sync
package using NPM and use the require()
method to pull the awesomeness from the module into your own code. That looked something like this:
var readline = require("readline-sync");
var name = readline.question("Please type your name: ");
By simply require
-ing the readline-sync
module, we were able to use all the goodness it had built in!
So we've already seen how to require()
other people's modules, let's see how to create and require()
our very own modules!
Creating a module
The most important thing to understand when it comes to creating your own modules is module.exports
.
module
is a global Node.js object that contains properties with some information about any given JavaScript file. It has properties like __dirname
, which is a path to the directory that the file is in, and __filename
which is the filename itself.
One of the properties on the module
object is called exports
. exports
begins as an empty object, and is just sitting there waiting for us to put our code on it so that it can be pulled in by a require()
function call somewhere else. This is how that looks in code:
// greeting.js
function greeting() {
console.log("Hello there!");
}
module.exports = greeting;
We set the module.exports object on this file to be a function expression (the code of the function itself, without actually calling it). Now we can require
this simple module from anywhere we want! Assuming we have another file (let's call it app.js
) that's in the same directory as greeting.js
, we can now do:
// app.js
var greeting = require("./greeting.js");
greeting(); // "Hello there!" prints to the console.
Because you'll almost exclusively be using modules that come from JavaScript files, you can actually leave out the .js
on the module name:
var greeting = require("./greeting");
However, you MUST keep the path to the module there (in the above case, ./
) if you are require
-ing a module of your own. The only time you can remove it is if you are requiring a Node.js built-in module, or something you've installed with NPM that is inside the node_modules
folder.
Conclusion
That pretty much covers the very basics of using Node modules in your projects. Remember that you'll need to require()
the module in every JavaScript file you wish to use that module in.
There's still a lot to learn about modules, so when you feel you've got a grasp on the basics, head over to Module Patterns to learn more about the most common ways to structure your modules!