How to create a NodeJS module?

I read the details on the NodeJS website: https://nodejs.org/api/modules.html . I don’t understand how modules work, and what are the minimum steps to create a module and how npm can help me.

How to create a module? How to use the module?
What does it mean to put it on npm?

Note. . This is a question to be answered in order to share knowledge as canonical.

+5
source share
3 answers

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

//mymodule.js module.exports = 3; 

Then you can load the module using require:

 //app.js require('./mymodule.js') 

I added './' because it is a single file module. We will review it later.

Now, if you do this, for example:

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

You can replace 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:

Each variable that you define inside the module will be defined only inside it:

 //mymodule.js var myPrivateVariable = 3; publicVariable = 5; // Never user global variables in modules //It 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:

Another thing you need to know about NodeJS modules is that if you use the same module twice (required), it will return the same instance, it will not work twice.

eg:

 //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, this private variable is shared between all instances of the module.

Module package

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

npm init will create a package.json file for you. There are 3 parts required for modules:

package.json

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

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

What you publish in NPM will be available to everyone. Therefore, never publish modules containing personal data. You can use private npm modules for this.

Next steps

Modules can return more than one function or one variable. See Examples 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; 

Additional Information:

Matters Related:

+13
source

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

You can think of a module as a set of functions that you can use in other code just by requiring it. for example: Consider a functional.js file that has content:

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

Now just require it in any other module, for example:

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

Exit: I am in the display function

Similarly, you can:

 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; 
+3
source

Create the npm command line package https://youtu.be/qfkwPpx1TIU

npm #npmpublish #nodejs #custompackage

0
source

Source: https://habr.com/ru/post/1244725/


All Articles