Node.js is an asynchronous
JavaScript library for building server applications that use the
CommonJS convention. All this syntax, modules at first confused me enough. But let's still try to figure it out.
Node.js v0.5.8-pre will be used in this article. Go!
')
To begin with, let's define a couple of functions that work with circles (probably you have already met them on the documentation pages):
var PI = 3.14
exports.area = function ( r ) {
return PI * r * r
}
exports.circumference = function ( r ) {
return 2 * PI * r
}
* This source code was highlighted with Source Code Highlighter .
Fine. Now we will try to use them in node. We start the console with the node command and connect our circle.js file. Notice that the path is a relative path. The extension can be omitted, node implies that it will be a js file. The first time you probably have something like this:
node> require ('./ circle')
{area: [Function], circumference: [Function]}
node> area
ReferenceError: area is not defined
at EventEmitter.anonymous (eval at readline (/usr/local/lib/node/libraries/repl.js:48:9))
at EventEmitter.readline (/usr/local/lib/node/libraries/repl.js:48:19)
at node.js: 845: 9What happened?
Let's figure it out. Having experience with other object languages, I am used to using the methods I previously defined. But with CommonJS modules, it's not that simple. Let's see how you can use our module:
node> var circle = require ('./ circle')
{area: [Function], circumference: [Function]}
node> circle.area (5)
78.5Fine. The result is already better. However, if you try the following:
node> circle.PI
node>We get “nothing”. After all, we correctly called the attribute circle, but received nothing. At first this may be somewhat confusing. Actually like me at the beginning. If you look at the documentation, then there is something to see about this.
To export an object, add to the special exports object. (Alternatively, one can use this instead of exports.)
We can rewrite our module as follows:
var PI = 3.14
this .area = function ( r ) {
return PI * r * r
}
this .circumference = function ( r ) {
return 2 * PI * r
}
* This source code was highlighted with Source Code Highlighter .
“And here is this?” - you ask. Yes, despite the fact that this is our object. We can present our module as follows:
function circle () {
var PI = 3.14
this .area = function ( r ) {
return PI * r * r
}
this .circumference = function ( r ) {
return 2 * PI * r
}
}
* This source code was highlighted with Source Code Highlighter .
Familiar? This is a regular JavaScript object. Thus, modules in Node.js are nothing more than objects familiar to us, defined in short form. That is, in fact, we declare only the body of the object. Now we have an idea about CommonJS modules.
Conclusion
Module files are nothing but regular objects. In the file, we define the module body itself. In CommonJS modules, if you want to make something accessible from outside, you must use
export . You can actually access PI (in the examples above), but this is the wrong approach. Using the global scope, we simply run the risk of overriding existing attributes or methods. And then sit and catch bugs. We export only public methods, all the rest is left inside the black box.
Export is an extremely convenient and practical approach.
From translator
This is a free interpretation of James’s record, rather than a full translation. The original of the post is already outdated and I tried to make some adjustments. I myself, in search of new technologies and horizons, went to node.js and try to understand it, as well as understand the reasons for its rapidly growing popularity. So I decided to share with the public some results of painstaking comprehension step by step.