Welcome!

@DevOpsSummit Authors: Yeshim Deniz, Zakia Bouachraoui, Pat Romanski, Liz McMillan, Elizabeth White

Related Topics: @DevOpsSummit, Java IoT, Microservices Expo

@DevOpsSummit: Blog Feed Post

Node.js ABCs - L Is for Loading | @DevOpsSummit #DevOps

A great benefit of utilizing a language like Node.js is the wide assortment of 3rd party modules that are at your disposal

One of the great benefits of utilizing a language like Node.js is the wide assortment of 3rd party modules that are at your disposal.  From calculating the validity of credit card numbers, to managing network connections, the Node Package Manager (npm) repository has hundreds of thousands of packages available to you.  This article will explain how you can load modules into your project.

Loading and Referencing a Module
In Node.js, modules are accessed either by their name or the file path on the system.  Non-system modules referenced by name will eventually be mapped into a file system path.

Each module exposes a public interface that can be used after the module is imported with the "require" into the current script.

var mod = require("some_module_name");

LoadingDockEquipment

The previous command will import and return an object that represents the external API exposed by the module.  The value of that object is dependent on the module and can include an object, an array, a function, or any other type of JavaScript object.

Developing and Exporting Modules
For the module developer, you must use the CommonJS Module System to make your modules available to the outside world.  Let's create a sample module “person.js” that exports a Person constructor.

/* File person.js */
function Person(name) {
var _name = name;
function name() {
return _name;
}
}
module.exports = Person;

In the above example, the Person function is defined and then specified with the "module.exports" object that the module will export to other scripts that require this module.

As mentioned above, more complex objects can be exported by adding onto the module.exports object with whatever values you deem fit for your module.

/* file mymodule.js */
function sayHi() { console.log("HI!!!"); }
function sayBye() { console.log("BYE!!!"); }
module.exports.sayHi = sayHi;
module.exports.sayBye = sayBye;

Below is the code to load the module and call the sayHi and sayBye functions.

var mymodule = require("./mymodule");
mymodule.sayHi();
HI!!!
mymodule.sayBye();
BYE!!!

Loading a Module
There are a couple of ways to reference modules depending on their kind.  There are core modules, 3rd party modules, or local modules.

Core Modules
Since core modules are part of the distribution, you need only refer to it by it's name (not the path).  Core modules with name collisions to local modules take precedence.  The following line will load a instance of the core "http" module.

var http = require('http');

File Module
A file module is a one that is defined in a single file on the file system.  The person.js example above is an example of a file module.  When loading a file module, you supply a path (full or relative) to the module file, optionally omitting the .js extension.

var mymodule = require("/home/joe/node/modules/mymodule");
var mymodule = require("./mymodule");

In those examples the loader will look for a file named "mymodule.js" in the specified directory.

Folder Modules
Like a file module, when loading a folder module, you specify the absolute or relative location of the folder.  The Node loader will assume this folder is a package and attempt to look for a package definition file (package.json) in which the name and location of the module file are located.  If no package.json file exists, the Node loader will look for a file named index.js.

Loader search procedure
If the module name is not a path and is not a core module, the loader will attempt to find it inside the special "node_modules" folder.  If the module doesn't exist there, it will continue up the path, folder by folder, looking within node_modules folders on those directories until either finds the module or hits the root of the file system.

Conclusion
That's about all there is to it for exporting and loading a Node.js module.  Creating modules is as easy as writing the module code and specifying what is returned with the module.exports object.  Then a simple call to require("module") is all the client has to write to make use of it.

Read the original blog entry...

More Stories By Joe Pruitt

Joe Pruitt is a Principal Strategic Architect at F5 Networks working with Network and Software Architects to allow them to build network intelligence into their applications.

@DevOpsSummit Stories
Dion Hinchcliffe is an internationally recognized digital expert, bestselling book author, frequent keynote speaker, analyst, futurist, and transformation expert based in Washington, DC. He is currently Chief Strategy Officer at the industry-leading digital strategy and online community solutions firm, 7Summits.
Addteq is a leader in providing business solutions to Enterprise clients. Addteq has been in the business for more than 10 years. Through the use of DevOps automation, Addteq strives on creating innovative solutions to solve business processes. Clients depend on Addteq to modernize the software delivery process by providing Atlassian solutions, create custom add-ons, conduct training, offer hosting, perform DevOps services, and provide overall support services.
Contino is a global technical consultancy that helps highly-regulated enterprises transform faster, modernizing their way of working through DevOps and cloud computing. They focus on building capability and assisting our clients to in-source strategic technology capability so they get to market quickly and build their own innovation engine.
The standardization of container runtimes and images has sparked the creation of an almost overwhelming number of new open source projects that build on and otherwise work with these specifications. Of course, there's Kubernetes, which orchestrates and manages collections of containers. It was one of the first and best-known examples of projects that make containers truly useful for production use. However, more recently, the container ecosystem has truly exploded. A service mesh like Istio addresses many of the challenges faced by developers and operators as monolithic applications transition towards a distributed microservice architecture. A tracing tool like Jaeger analyzes what's happening as a transaction moves through a distributed system. Monitoring software like Prometheus captures time-series events for real-time alerting and other uses. Grafeas and Kritis provide security polic...
DevOpsSUMMIT at CloudEXPO will expand the DevOps community, enable a wide sharing of knowledge, and educate delegates and technology providers alike. Recent research has shown that DevOps dramatically reduces development time, the amount of enterprise IT professionals put out fires, and support time generally. Time spent on infrastructure development is significantly increased, and DevOps practitioners report more software releases and higher quality. Sponsors of DevOpsSUMMIT at CloudEXPO will benefit from unmatched branding, profile building and lead generation opportunities.