Johan Coppieters - Lector Web Development Howest - Brugge
In this short note I try to explain the usage of the 'module.exports' object when using 'require' in a nodejs script. There is also a shorthand 'exports', which is an alias to 'module.export', don't use is, it is confusing.
In your module you can add something to the object 'module.exports'. The require function in a script using your module, will return this same object (with all the things in it, that you added, variables, functions, objects, ...)
in a module -> module.exports is a reference to an object
in a script -> require("module-filename") returns this object
Exporting functions
Defining a module
src/myModule.js
module.exports.turnOn = turnOn; module.exports.turnOn = turnOff; module.exports.isOn = isOn; var switchState = false; function turnOn() { switchState = true; } function turnOff() { switchState = false; } function isOn() { switchState switch; }
Using this module
src/example.js
var aBulb = require("./myModule.js"); console.log("bulb is on " + aBulb.isOn()); aBulb.turnOn(); console.log("bulb is on " + aBulb.isOn());
The better object oriented way
If you want to return a single function from a module definition (for example a contructor function), you can directly set the exports variable
src/myModule1.js
module.exports = Lighting; function Lighting() { this.switch = false; } Lighting.prototype.turnOn = function () { this.switch = true; }; Lighting.prototype.turnOff = function () { this.switch = false; }; Lighting.prototype.isOn = function () { return this.switch; };
Using this module / object definition
src/example.js
var Bulb = require("./myModule1.js"); var aBulb = new Bulb(); console.log("bulb is on " + aBulb.isOn()); aBulb.turnOn(); console.log("bulb is on " + aBulb.isOn());
A complete library
If you have a number of modules you want to expose, put them all into a directory (example "myMods") and add a file index.js
src/myMods/index.js
module.export.module1 = require("./myModule1.js"); module.export.module2 = require("./myModule2.js");
In myModule 1 and 2.js just do as above.
Using these modules: in example.js which sits in a directory containing the myMods directory (if not, specify a different path, for example: "../libs/myMods")
src/example.js
var myMods = require("./myMods"); var aBulb = new myMods.Bulb(); console.log("bulb is on " + aBulb.isOn()); aBulb.turnOn(); console.log("bulb is on " + aBulb.isOn());
4 comments:
Great, this came very handy to me in regards of taking profit of Node.js modules system.
var Class = function(json) {
}
module.exports = Class;
Brilliant !
Good blog post!.
I got some issues when following your post. Did you run the code before posting?
in src/myModule.js
var switch = false; //"switch" is a key word.
Thus I changed "switch" to "s" and
"module.exports.turnOn = turnOff;" to "module.exports.turnOff = turnOff;". Then continue ...
Thank you!
Very helpful tutorial!
You kept it really to the thing you wanted to explain and did a good job with that. I understood it right away... Thank you!
@Gihan: always forgot, but yes, you're right 'switch' is a reserved word, I've changed it.
Thanks you,
Johan.
Post a Comment