CommonJS模塊
// Invoke 'strict' JavaScript mode
'use strict';
// Define a module variable
var message = 'Hello';
// Print message to the console
exports.sayHello = function() {
console.log(message);
};
// Invoke 'strict' JavaScript mode
'use strict';
// Load the 'hello' module
var hello = require('./hello');
// Use the 'hello' module sayHello() method
hello.sayHello();
// Invoke 'strict' JavaScript mode
'use strict';
// Define the module method
module.exports = function() {
// Define functional variable
var message = 'Hello';
// Print the message variable to the console
console.log(message);
};
// Invoke 'strict' JavaScript mode
'use strict';
// Load the 'hello' module
var hello = require('./hello');
// Call the 'hello' module as a function
hello();
在加載模塊時可以省略.js拓展名,Node會先尋找同名的文件夾,如果找不到,則尋找同名的js文件。