Create a simple node.js module

Node.js is a great JavaScript framework for easily building fast, scalable network applications. Not just I say so, but according to the website:

Node.js® is a platform built on Chrome’s JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.

node.js provides a complete solution for server-side applications, such as web platforms. It can communicate with other systems, like a database, LDAP or any other legacy application. There are many scenarios we could choose to implement node.js.

Node.js Module

node.js provides modules to help any development project, this helps to reduce development time because it negates the need to write many lines of code, because there are tens of thousands of modules in the npm registry to help in addressing common recuring problems.

It is also possible to develop your own module for node.js. For instance, consider you have a function and you may have a requirement to call this functions many times in many files across your application. We could write a function once in a module once in a module , then it will be called by simply attaching the module to a file.

The javascript module pattern is widely used because it provides structure and helps organize your code as it grows. Unlike other programming languages, JavaScript doesn’t have a special syntax for packages, but the module pattern provides the tools to create self contained decoupled pieces of code, which can be treated as block boxes of functionality and added, replaced or removed according to the requirements of the software.

Create a Simple Module

I’ll illustrate how to write a very simple function then it will be exported as a module. It is not going to be a particulalry earth shattering and useful module but it will be suffice to prove a point.

Create a javascript file and call it GreetingModule.js

This file is basically going to be our fab and groovy module we want to call.

We now need to create a second javascript file call it greet.js

The above code essentially references our previously created module, we use the require keyword, which needs the full path of the module. ‘./’ means the module has the same location as the caller.

We can now go to the command line and navigate to our directory where out files are kept and call:

In my case I just use webstorm in the terminal window, which displayed the following result.

Although our little module doesn’t actually do a whole lot, it does involve quite a few key JavaScript concepts and patterns and how to implement them.

Gary Woodfine

Freelance Full Stack Developer at
Helps businesses by improving their technical proficiencies and eliminating waste from the software development pipelines.

A unique background as business owner, marketing, software development and business development ensures that he can offer the optimum business consultancy services across a wide spectrum of business challenges.