Node Dependency Management (Part2)

Introduction

In my previous post about Nodejs – Dependency Management, we looked at basics of what is Nodejs module. How can we use module.exports and require to work with dependencies. How folder dependencies work and why they are useful. We all look at how Nodejs lookup system works to look for dependencies and some basics of NPM.

Today we will continue from that point and see how we use this module system to breakdown an application in multiple modules. What are the benefits of doing so and and how Nodejs handles this.

I will be using some of the learnings from previous post, so, if you are here first time, I will recommend to check the previous post to make this part easy to understand.

But before we continue about application setup with multiple modules, lets learn a few more other interesting aspects of Nodejs modules.

Are Node Modules Singleton?

In previous post, we talked about that Node loads a module only once and if someone ask for it, Node gives the cached copy of that module. So, yes they behave like singletons. Let’s see an example of this behavior.

To start with I created a project for the application, initialize the application and created a file user.js as follows:

Now, in app.js I require the user module and used it as follows:

You can see that we created two variables of user, however, changes make to one are reflected to other. The user module is cached and reused when asked for another time. This is something you must be aware of.

Let’s see how it behaves if we change it to a constructor function:

Constructor Function

I have changed the implementation of user.js (we learned about constructor function in previous post)

and now I used it in app.js as follows:

So, this time, we are getting two different instances and now you can see the difference. Don’t worry about this too much, we will see some more examples and solutions in later posts.

Now, let’s move to our main topic, about managing applications using modules.

Application Structure

We will start building up foundation for our application structure, we will start simple and refine it as we move forward.

I have created few new folders and moved the files around and now we have following structure in our application:

The first thing I did is create separate folders for different modules. You can see that there is a book-management folder, another folder for logger and similar to that user-management. This way each module is focused and easy to locate and manage.

Each folder can have sub-folders and so on. Also, notice the index.js file at folder level, this will serve as an API for the module.

Then, in app.js, we can reference the modules by requiring them and you can see that it works as expected. Notice how we can reference user.js by requiring it.

Now, lets update our code:

I created few items under book-management module (folder). Here you can see that book.js is following a very common JavaScript constructor pattern (it is a little bit different from constructor function example we saw above with user.js)

then we have following code in corresponding index.js file:

so, now we have index.js acting as an API for the module. Lets use it in app.js as follows:

And we can see that our application is working as expected. I went ahead and changed the user.js to follow the same constructor pattern of book.js and you can also try this as an exercise.

One thing I would like to mention that you can use your existing JavaScript knowledge or design patterns skills from other languages in Node.js. The constructor pattern we saw earlier is one of the example. As these patterns have different implementations, so depending on your requirement and /or your preferences, you can use those as you see fit.

Here, is how our code looks after refactoring user.js part:

Summary

Our application is very basic and in this post we started setting up the foundation by learning some of the basics of Nodejs dependency management and some common ways of setting up your project structure.

We also saw that we can use our existing knowledge of JavaScript design patterns in our Node applications. We saw some few simple examples of these as well.

My idea is to extend this learning into a more close to real world project and add more features and functionality to it gradually. I will try to write more in coming days on this topic. Let me know if you have some comments or questions. You can get the code from this git repo. Till next time, Happy Coding.

My Recent Books


Discover more from Hex Quote

Subscribe to get the latest posts sent to your email.

Discover more from Hex Quote

Subscribe now to keep reading and get access to the full archive.

Continue reading