Node.js Using a simple module in node.js


What is a node.js module (link to article):

A module encapsulates related code into a single unit of code. When creating a module, this can be interpreted as moving all related functions into a file.

Now lets see an example. Imagine all files are in same directory:

File: printer.js

"use strict";

exports.printHelloWorld = function (){
    console.log("Hello World!!!");

Another way of using modules:

File animals.js

"use strict";

module.exports = {
    lion: function() {


File: app.js

Run this file by going to your directory and typing: node app.js

"use strict";

//require('./path/to/module.js') node which module to load
var printer = require('./printer');
var animals = require('./animals');

printer.printHelloWorld(); //prints "Hello World!!!"
animals.lion(); //prints "ROAARR!!!"