In this video tutorial we shall see how to write and make use of local modules in Node.js application.
This is a simple tutorial to demonstrate writing of local module
Local Module File
exports.students = [ 'Satish', 'Kiran', 'Sunitha', 'Jyothi' ];
exports is a global provided by node.js
students is a name given by us; it’s a property name and we assign an array to it which contains some names.
1 2 3
var students = require('./students'); console.log(students.students);
We require the local module students in appStu.js file.
./ (dot followed by a forward slash) specifies that it’s a local module and it need not search for the file in core module or node module collection.
Next, object.property name will print out the entire array present inside the local module file.
Local Module: Node.js
One of the main advantages of local modules is, the reusability of code. It also helps in separating specific logic and keeping the code clean.