// Modules, require() and NPM

Outline

* Using modules in application.
* Three most commmon sources of Node modules.
* create and publish your own Node modules.

// Using modules in your application
Modules are the way you bring external functionality into your application.
var foo = require('foo');
var Bar = require('bar');
var justOne = require('largeModule').justOne;
the require function loads a module and assigns it to a variable for your application to use.

var f = 2 + foo.alpha <--- Modules can export variables.
var b = foo.beta() * 3 <--- Modules can export functions.
var bar = new Bar(); <-- Modules may export objects.
console.log(justOne()); <---- Import Just one function you need.

// 3 Sources of Node Modules.
#1. Built-in Modules (setTimeout and setInterval)
    Come pre-packaged with Node.
    Are require()'d with a simple string identifier.
        var fs = require('fs');
    A sample of built-in modules include:
        fs -> for accessing the file systems.
        http -> for creating and responding to http requests.
        crypto -> for performing cryptographic functions.
        os -> for accessing attributes of the underlying operating system.


#2. File-Modules.
Your Project Files.

    Each .js file is its own module.
    A great way to modularize your application's code.
    Each file is require()'d with file system-like semantics:
        var data= require('./data.js'); (Get the functionality of data.js file in the same directory.)
        var foo = require('../other/foo.js'); ( foo.js is the 'other' sub-directory).
        var bar = require('../lib/bar.js'); (or in some navigable directory).
    You may get a single variable using require() like this.
    var justOne = require('./data.js').justOne;
    The way you make variables available for other JavaScript files is by assigning values to modules.export object.
    
    // one.js
    
    var count = 2;
    var doIt = function (i, callback) {
     // do something. invoke callback
    }
    modules.export.doIt = doIt; // making the doIt function available outside of one.js
    modules.export.foo = 'bar'; // making the foo variable value available outside of one.js.
    
    // two.js
    var one = require('./one.js');
    # as doIt got exported using modules.export    
    one.doIt(23, function(err, result) {
        console.log(result);
    });
    # as foo variable got exported using modules.export.
    console.log(one.foo);
    # You cannot use this, as it never got exported from one.js
    console.log(one.count);

#3. NPM packaged Modules (3rd Party Modules)
    Installed via "npm install module_name" into "node_modules" folder, inside your project directory.
    Are require()'d via simple string identifiers, similar to built-ins'
        * var request = require('request')
    Can require() individual files from within a module, but be careful.
        * var BlobResult = require('azure/lib/services/blob/models/blobresult');
    Some node modules provide more than variables and functions, you may want to access. They may provide utilities you can invoke from command line.
    Install these modules with "npm install -g module_name". (Since these modules have scope beyond any one application, you can chose to install them globally).
        Examples Include:express web framework, mocha test framework, azure-cli.
        
        
You can publish your own modules to npm registry and it is very easy.
The most essential thing, that you need to do, if somebody wants to use your module by downloading from npm registry, is to have a package.json file.
As this is the file that directs the npm to setup your node module.
In this file, you specify, various dependencies, link it to your Git Hub if it exists and so on.
Sample "package.json" file looks like below:-

{
"name":"NodeJSLearnerFiles", //required
"version":"0.0.1", //required
"author":"Sai Pavan Malrakunta",
"description":"Very Good Beginnings if you want to Learn Node.js",
"keywords":["NodeJS","Learn","Files"],
"repository": {
    "type":"git",
    "url":"https://github.com/smarlaku820/NodeJSLearnerFiles.git"
    },
"dependencies": {
"underscore":"1.4.x",
"request":">=2.1.0"
},
"main":"lib/cool.js" // Main is where you define the entry point into your module. This is what is executed when someone requires your module.
}

// npm add user (to create an account on the NPM registry)
// npm publish . (from within project root).
// npm install "module_name" (from an empty directory) -- verify it.