Node.js, Require and Exports
- Transfer
When I just started playing with Node.js, there was only one thing for me that made me uncomfortable. Interesting, but I'm talking about
In Node, all pieces are visible to each other only within the same file. By pieces, I mean variables, functions, classes, and their members. For example, we have a file with the
Familiar access to a variable
Now, before we learn how to make these things visible outside the module, we will examine in more detail how the modules are loaded into Node.js. Now we will talk about the place where it is written
Of course, as long as our module does not give anything back , all the above examples are useless. And in order for our module to give something away , we will use
Now our module has become much more useful:
There is still such a way to give things from our module:
The difference between these approaches is not great, but important. As you can see, in this case we export the function directly:
All this to the fact that then it will be easier to use:
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.
To further illustrate the interaction of modules, let's look at the following example:
When you connect this module using
Which, in essence, is a simplified notation for the following code:
I hope these examples help you get comfortable!
module.exports
. Interestingly speaking , I hint that this, after all, is one of the fundamental parts of Node.js and it is quite simple. Now, looking back, I can’t explain why it stopped me so ... I just remember that this moment was not obvious to me. Well, I believe that I am one of those many who met him once or twice, at first only got confused before writing anything by applying it. In Node, all pieces are visible to each other only within the same file. By pieces, I mean variables, functions, classes, and their members. For example, we have a file with the
misc.js
following contents:var x = 5;
var addX = function(value) {
return value + x;
};
Familiar access to a variable
x
and function addX
from another file is not possible. This has nothing to do with usage var
. The fact is that Node consists of blocks called modules , and each separate 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, we will examine in more detail how the modules are loaded into Node.js. Now we will talk about the place where it is written
require
. require
they are used to load a module, usually assigning the result of its operation to some variable:var misc = require('./misc');
Of course, as long as our module does not give anything back , all the above examples are useless. And in order for our module to give something away , 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 still such a 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 to the fact that then 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.
To further illustrate the interaction of modules, let's look at 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, which allows you to do the following:require('./powerlevel')(9050);
Which, in essence, is a simplified notation for the following code:
var powerLevel = require('./powerlevel')
powerLevel(9050);
I hope these examples help you get comfortable!