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.

Affiliate Disclaimer

Disclosure: Please note that some of the links included in the blog posts are affiliate links, which means I will earn a commission if you decide to make a purchase.

I only include affiliate links to products, services and companies that I have personal experience and have actually used. I only recommend them because they are helpful and useful, not because of the small commissions I make if you decide to buy something.

Please do not spend any money on these products unless you feel they are relevant, suitable and will help you achieve your goals.

Buying anyone of these products and the commisions earned will not make me an overnite multi millionaire, but they will help to cover the hosting costs and compensate for the countless hours and effort I put in to create meaningful and valuable free content I provide to help others succeed.

You've also undoubtedly noticed that I also use various advertising networks - unless of of course you're using an Ad blocker, this is also an attempt to reduce my monthly deficit I experience in providing free relevant, valuable and on occassion insightful content for the benefit of others.

I only really make some money if people click on the ads, unless of course you consider 1c per 1000 impressions real money!

Bear in mind just how many impressions I need to make before I can cover my £20 hosting costs a month!

If you are using an adblocker and guilt stricken you can of course donate using any of the buttons below, but I won't be holding my breath.

Buy me a coffeeBuy me a coffee