Organize NodeJS Code in Custom Modules

Everything can look clean with NodeJS modules

Ivano Di Gese
Jul 30, 2019 · 3 min read

When you start developing Node apps, everything looks simple, plain and well coded. Until you start implementing ExpressJS routes, general features, and common tasks, at which point everything starts looking like a mess. JavaScript has always suffered from code organization and separation of context issues, but with NodeJS modules it can all look clean and well coded.

Image for post
Image for post
Photo by Kaleidico on Unsplash

NodeJS Modules

NodeJS modules are wrappers of code, methods, models and more. A NodeJS module is simply a container of logical independent routines or code parts. The way you split up code using modules is strictly dependent on how you want to organize your code. NodeJS modules constitute a principle of separation of concerns, context and of reuse of code. As soon as you can split up code in the correct way, you essentially design NodeJS modules that you can reuse simply, any time. You can extend this kind of implementation and conception, and maximize the benefits of NodeJS modules, with lazy loading.

Since NodeJS modules make able to encapsulate almost any behavior in separated parts of codes, you can decide whether or not to load resources, etc. It’s up to you.

The Magic Word: module.exports

Everything in a NodeJS module is essentially private. Whenever you declare a new variable or a new method, NodeJS considers that useful within the module itself — there is nothing exposed to outside. That’s why in NodeJS we can decide what should be exposed to the rest of the world by using the module.exports.

A module can export almost everything: variables, constants, functions, even objects. We know JavaScript is versatile and easy to manipulate, so it’s easy to make module.exports to even expose objects with methods inside its definition.

The first step while approaching a custom NodeJS module is usually something simple, like this:

module.exports = “hello from a module”;

This makes a NodeJS module composed of a string. If you import this module, all you have is a string:

// main.jsvar message = require(“./hello.js”);

You can extend the same pattern by exposing a function more than a string:

// functions.jsmodule.exports = function(name){     return “Hello “ + name;}

In this case, whenever you import the module, you get back a function that looks like a method:

// main.jsvar hello = require(“./functions.js”);console.log(hello("Ivano"));

Exporting Objects

The way you can export and import custom NodeJS modules is easy to code. It’s common to need to expose several methods and multiple variables in a custom module — how do we do that?

It’s easy. Just extend the same concept and the same syntax by exporting an object, more than a single variable or method. By using a standard JSON notation, you can create an object on the fly and expose anything you want to the rest of the world:

Conclusions: Beyond Common Uses

Using NodeJS modules is a lot of fun and pretty straightforward. Practice some exports and you’ll soon master composing NodeJS custom modules.

You should probably do some “private” configuration, then export just part of the login inside the module. That’s common and makes NodeJS modules even more powerful and adaptive. Composing private and public portions of code is the main goal of composing custom NodeJS modules. In Mongoose, for example, you would define the Schema inside the module and then export just the Mongoose Module to the main.

Another good example is to code a NodeJS module to share the instance of a MongoDB connection. This is much better than globalizing or sharing the connection by putting it in the ExpressJS request (which is popularly implemented).

For ExpressJS routes, I recommend using ExpressJS router instance in different files: just check the official documentation.

Defining many ExpressJS routes it’s almost mandatory for medium to big applications and you can’t pretend to stub all of them in the same Javascript file!

Better Programming

Advice for programmers.

By Better Programming

A weekly newsletter sent every Friday with the best articles we published that week. Code tutorials, advice, career opportunities, and more! Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Ivano Di Gese

Written by

Passionate IT skills on the run: keep calm, do your stuff and code better

Better Programming

Advice for programmers.

Ivano Di Gese

Written by

Passionate IT skills on the run: keep calm, do your stuff and code better

Better Programming

Advice for programmers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store