Node has a simple module loading system. In Node, files and modules are in one-to-one correspondence. As an example, app.js
loads the module hello.js
and custom_goodbye.js
in the same directory.
//hello.js
var hello = function(){
console.log("hello");
}
module.exports = hello;
//custom_goodbye.js
exports.goodbye = function(){
console.log("bye!");
}
//app.js
var hello = require('./hello');
var gb = require('./custom_goodbye');
hello();
gb.goodbye();
var the_module = require('./path_module'); // look in same directory var the_module = require('../path_module'); // look in parent directory var the_module = require('/path_module'); // look in absolute path
create package.json filename like this following code:
{
"name": "my-application-name",
"description": "Try to learning nodejs",
"version": "0.0.1",
"author": {
"name":"Andy",
"email":"sintret@gmail.com"
},
"dependencies" : {
"connect":"~1.8.7"
}
}
$ npm install
install into the node_modules directory : my_app/node_modules/connect
"connect" : "1.8.7" explains => 1 = major 8 = minor 7 = patch Ranges "connect" : "~1" is same >= 1.0.0 < 2.0.0 "connect": "~1.8" is same >= 1.8.0 <1.9.0