Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do I create a NodeJS Module?

Tags:

node.js

I have read the details on NodeJS site : https://nodejs.org/api/modules.html. I don't understand how modules work, and what are the minimal steps for creating a module, and how npm can help me.

How can I create a module?
How do I use a module?
What does putting it on npm mean?

Note: this is a self answered question, with the purpose of sharing knowledge as a canonical.

like image 542
Aminadav Glickshtein Avatar asked Mar 09 '16 10:03

Aminadav Glickshtein


2 Answers

You can create a NodeJS module using one line of code:

//mymodule.js

module.exports = 3; 

Then you can load the module, by using require:

//app.js

require('./mymodule.js')

I added './' because it is a module of one file. We will cover it later.

Now if you do for example:

var mymodule = require('./mymodule.js');
console.log(mymodule); // 3

You can replace the number 3, with a function, for example:

//mymodule.js:
module.exports = function () {
  console.log('function inside the module');
};

Then you can use it:

var mymodule = require('./mymodule.js');
mymodule();

Private variables:

Every variable you define inside A module will be defined only inside it:

//mymodule.js
var myPrivateVariable = 3;
publicVariable = 5;   // Never user global variables in modules
                      //It's bad-pracrtice. Always add: var.
module.exports = function() {
  // Every function of the module can use the private variables
  return myPrivateVariable++
};

//app.js

var mymodule = require('./mymodule.js');
console.log(mymodule()); // return 3
console.log(mymodule());  // return 4

Reuse modules:

One more thing you need to know about NodeJS modules, is that if you use the same module twice(require it), it will return the same instance, it will not run in twice.

for example:

//app.js

var mymodule1 = require('./mymodule.js');
var mymodule2 = require('./mymodule.js');
console.log(mymodule1()); //return 3
console.log(mymodule2()); //return 4 (not 3)
console.log(mymodule1()); //return 5 

As you see in the example below, that private variable is shared between all the instances of the module.

A module package

If your module contain more than one file, or you want to share the module with others, you have to create the module in separate folder, and create a package.json file for the module.

npm init will create package.json file for you. For modules, there are 3 required parts:

package.json

{
  "name" : "You module name",
  "version" : "0.0.3"
}

Now, you can publish the module, using npm publish. I recommend you publish all your modules to github as well, then the module will be connected to your github page.

What you publish to NPM will be accessible by everyone. So never publish modules that contain private data. For that you can use private npm modules.

Next steps

Modules can return more than one function or one variable. See this samples in which we return an object.

module.exports.a = function() {
  // ..
};
module.exports.b = function() {
  // ..
};
  
// OR

myObj = {
  a:3,
  b:function() {
    return this.a;
  }
};
module.exports = myObj;

More info:

  • Read about package.json files
  • Versioning in you modules best practice
  • More best practive for NodeJS modules
  • Private modules, using private npm

Related Questions:

  • What is the purpose of Node.js module.exports and how do you use it?
  • module.exports vs exports in Node.js
like image 131
Aminadav Glickshtein Avatar answered Oct 17 '22 07:10

Aminadav Glickshtein


Creating module in node.js is pretty simple!!!

You may consider module as a set of functionalities you can use in other code by simply just requiring it. for eg:Consider a file functional.js having the content:

function display(){
    console.log('i am in a display function');
    }
module.exports = display;

Now just require it in any other module like:

var display = require('./functional');
display()

Output:i am in a display function

Similarly you can do:

var exports = module.exports = {};
exports.display = function(){
    console.log('i am in the display function');
}

or you do the same for objects like:

var funObj = {
    hello:function(){
            console.log('hello function');
          },
    display:function(){
            console.log('display function');
          }
};
module.exports = funObj;
like image 37
Hiro Avatar answered Oct 17 '22 09:10

Hiro