Create file with folder
- We need to create an external file and use module.exports. We can emit module.
- Create a new folder and the a new file named as index.js
- Create a function which accepts an input parameter as string and then logs it to the console
//Pass the message and then log it to the console
module.exports.log=function(msg){
console.log(msg);
}
Refer the file using require.js
- Specify the path of the JavaScript file to be referred in the file'
- We are not specifying the file name. Based on the name specified in require it would look if there is any existing folder or else it would look for the file.
- Now we can directly use the function as shown below
//Folder dependencies. Based on the name specified in require it would look
//if there is any existing folder or else it would look for the file.
var logger=require("./logger");
logger.log("this is from the log");
- Run the JavaScript code in node
Output
c:\Development\NodeProjects\vanilanode>node app
Reference other file functions in node
this is from the log
No comments:
Post a Comment