Node JS: Create User Defined Module


From the first blog, we are using predefined modulus. Either, it is fs or http.

Unlike previous article, this time we are going to use user-defined module.

And, it won’t be a genius piece of code. But, we will use this concept in our latter projects where you divide your projects code in different file (which is called individual modules).


In this blog, we will code to node file and use one of them in another file.

So, let’s start the coding part.


Save it as my_module.js

Nothing new with this code, except the export list which decides the accessibility of your function. Module (which point to the page), then exports (which, implicitly decides the foreign accessibility) and last, we have showFn (which is user imagination, by which name he wants to call his function). And, on the right side we always put the local function (desired function, which you want to access).

And, we are going to use this in our next code,

desired function

And, in the first we import module in the current code. Root (./) and then, you can continue with the path of the file. Suppose you have you module in lib folder then, it would be like ./lib/my_module.js.

Rest is quite understood.

And, you are expecting something like this:



So, this time we have discovered about user-defined module. Next, we will try to put our own module to npm online server so other can use it easily. It does not need any extra effort.