How to Import Your Own Modules in Node

In order to create your own modules in Node, you attach your own classes or functions to an exports or module.exports object. Then you can access those items in the required module. Suppose we have a file named classes.js. If we wanted to import the ClassX and ClassY classes from that module, then we would add the following lines at the end of the classes.js file:

module.exports.ClassX = ClassX ;
module.exports.ClassY = ClassY ;

Then, in another file we would access these classes as:

var classes = require("./classes.js") ;
var ClassX = classes.ClassX ;
var ClassY = classes.ClassY ;