Local Module: Node.js

In this video tutorial we shall see how to write and make use of local modules in Node.js application.

In previous video tutorials we’ve seen the procedure for writing and using built-in Node.js modules.
HTTP module.
readline Module.

This is a simple tutorial to demonstrate writing of local module

Local Module File
students.js

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.

Application File
appStu.js

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



YouTube Link: https://www.youtube.com/watch?v=qh2e_9AmakI [Watch the Video In Full Screen.]



Note:
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.

View Comments

  • Also note that: console.log(students.students[0]); fetches the first name in the array!
    Loop through the array to print all the elements of the array, by incrementing the index.