Monday, February 13, 2017

Using export in node




  • We need to create an external file and use module.exports. We can emit module.
  • Create a property named as firstName and assign string values as shown below

//We can also specify it as module.exports
exports.firstName="My firstName is Prathap"



  • Specify the path of the JavaScript file to be referred in the file
  • Use the property from the referenced JavaScript file

         Note: Require functionality loads it only once

console.log("Reference other file functions in node");
//Refer other javascript file. dependency management
var greetings=require("./referencefile.js");
//show the value of the first name in the console
console.log(greetings.firstName);


  • Run the JavaScript code in node

c:\Development\NodeProjects\vanilanode>node app
Reference other file in Node
My firstName is Prathap

No comments:

Post a Comment