In this post I will show you how you can create your own custom module in Nodejs. You might have been using Utilities, Colors etc modules in nodejs, like this you even create your own custom Modules.
To create our own custom module in Nodejs we have to do two things, i.e. its a two step process :
exports.display = function(str) {
console.log(str);
});
We need to export all the exposed functions using the exports object as shown above.For Second step :
var disp = require ('./myfunctions.js');
console.log(disp.display('My Function Display'));
At first we have to import it in current session using require and assigning the library to a variable name. Once assigned we can call any of the exposed functions in our code as shown above.
Please Like and Share the Blog, if you find it interesting and helpful.
To create our own custom module in Nodejs we have to do two things, i.e. its a two step process :
- Using exports object to export the function.
- We will create a variable of the full library treating it as single imported object, in order to access the functions.
exports.display = function(str) {
console.log(str);
});
We need to export all the exposed functions using the exports object as shown above.For Second step :
var disp = require ('./myfunctions.js');
console.log(disp.display('My Function Display'));
At first we have to import it in current session using require and assigning the library to a variable name. Once assigned we can call any of the exposed functions in our code as shown above.
Please Like and Share the Blog, if you find it interesting and helpful.
No comments:
Post a Comment