As the article header says, it was a good effort by the author to help the readers to understand Node.js module exports:
https://stackify.com/node-js-module-exports/
Showing posts with label module.exports. Show all posts
Showing posts with label module.exports. Show all posts
Monday, April 13, 2020
Sunday, April 5, 2015
module.exports in Node.js
Suppose you want to write a module, say a functions module and access it in another module, say main.js, how are you going to do it in Node.js?
Real simple!
Create two .js files under C:\nodetest folder: functions.js, main.js
functions.js
module.exports = { addNumbers : function() { var total = 0; for (var a = 0; a < arguments.length; a++){ total += parseInt(arguments[a]); } return total; }, findArea : function(r){ return 3.142 * r * r; } };
functions.js:
this.addNumbers = function() { var total = 0; for (var a = 0; a < arguments.length; a++){ total += parseInt(arguments[a]); } return total; }; this.findArea = function(r){ return 3.142 * r * r; };
main.js
var functions = require('./functions.js'); console.log(functions.addNumbers(1,2)); console.log(functions.findArea(2));
Run the main.js from command prompt!
C:\nodetest>node main
Reference:
http://www.sitepoint.com/understanding-module-exports-exports-node-js/
Real simple!
Create two .js files under C:\nodetest folder: functions.js, main.js
functions.js
module.exports = { addNumbers : function() { var total = 0; for (var a = 0; a < arguments.length; a++){ total += parseInt(arguments[a]); } return total; }, findArea : function(r){ return 3.142 * r * r; } };
or
this.addNumbers = function() { var total = 0; for (var a = 0; a < arguments.length; a++){ total += parseInt(arguments[a]); } return total; }; this.findArea = function(r){ return 3.142 * r * r; };
main.js
var functions = require('./functions.js'); console.log(functions.addNumbers(1,2)); console.log(functions.findArea(2));
Run the main.js from command prompt!
C:\nodetest>node main
Reference:
http://www.sitepoint.com/understanding-module-exports-exports-node-js/
Subscribe to:
Posts (Atom)