📜 ⬆️ ⬇️

Node.js, Require and Exports

When I just started playing with Node.js, for me there was only one thing that caused me discomfort. Interesting, but I'm talking now about module.exports . Speaking of amusing , I hint that this is, after all, one of the fundamental parts of Node.js and it is quite simple. Looking back now, I cannot explain why it stopped me so much ... I just remember that this moment was not obvious to me. Well, I suppose that I am one of those many who, having met him once or twice, at first only got confused, before writing something using it.

In Node, all things are visible to each other only within the same file. By pieces, I mean variables, functions, classes, and their members. For example, we have the file misc.js follows:

 var x = 5; var addX = function(value) { return value + x; }; 

The usual access to the variable x and the function addX from another file is not possible. This has nothing to do with the use of var . The fact is that Node consists of blocks called modules , and each individual file is inherently a separate block, whose scope is isolated from other similar blocks.
')
Now, before we learn how to make these things visible outside the module, let's take a closer look at how modules are loaded into Node.js. Now we will talk about the place where require is written. require use to load a module, usually assigning the result of its work to some variable:

 var misc = require('./misc'); 

Of course, as long as our module does not give anything, all the examples given are useless. And to give our module something, we will use module.exports :

 var x = 5; var addX = function(value) { return value + x; }; module.exports.x = x; module.exports.addX = addX; 

Now our module has become much more useful:

 var misc = require('./misc'); console.log(" %d  10   %d", misc.x, misc.addX(10)); 

There is another way to give things from our module:

 var User = function(name, email) { this.name = name; this.email = email; }; module.exports = User; 

The difference between these approaches is not great, but important. As you can see, in this case we export the function directly:

 module.exports.User = User; //vs module.exports = User; 

All this means that later it will be easier to use:

 var user = require('./user'); var u = new user.User(); //vs var u = new user(); 

The benefit of using this method is that in this case it does not matter to us whether your module will represent a container with exported values ​​or not.

In order to present the process of interaction between modules even more colorfully, let's consider the following example:

 var powerLevel = function(level) { return level > 9000 ? "it's over 9000!!!" : level; }; module.exports = powerLevel; 

When you connect this module using require , in fact it will be a function that allows you to do the following:

 require('./powerlevel')(9050); 

What, in essence, is a simplified notation of the following code:

 var powerLevel = require('./powerlevel') powerLevel(9050); 

I hope these examples will help you get started!

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


All Articles