- Sep 30, 2018
- admin
- 0
Node.js Local Module:
Node.js also facilitates its users to create their own modules using export keyword. Local modules are the user-defined modules which are created locally in a Node.js application. These local modules can also be mexican steroids online packaged and distributed via NPM.
- Exports:
The module.exports object is a special type of object which is used to expose a function, object or variable as a module in Node.js.
➔ Creating a Local Module:
- Create a module in a separate JavaScript file.
- Assign the object of the module to exports.
- Including a Local Module:
To include a local module in an application, Node.js require() function with the path of the module is used.
Syntax:
var variable_name = require(‘module_path’);
Example:
Module: mod.js
var mod = {
msg: function (msg) {
console.log(‘Msg: ‘ + msg);
},
};
module.exports = mod
Application: hello.js
var hello = require(‘/home/Desktop/modu.js’);
hello.msg(‘HELLO’);
OUTPUT:
Msg: HELLO