Export Module in Node.js



Export Module in Node.js:

Node.js also facilitates its users to create their own modules using export keyword. The module.exports or exports object is a special type of object which is used to expose a function, object or variable as a module in Node.js.

●     Export Literals:

If a string literal is assigned to the module.exports or exports object, the string literal will be exposed as a module.

●     Export Object:

If a property or method is assigned to the module.exports or exports object, than that property or method will be exposed as a module.

●     Export Function:

If a function is assigned to the module.exports or exports object, than that function will be exposed as a module.

●     Export function as a class:

If a class is assigned to the module.exports or exports object, than that class will be exposed as a module.

 

Including a Local Module in an application:

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

 

Tags: ,
Leave a comment

Your email address will not be published. Required fields are marked *

Subscribe now

Receive weekly newsletter with educational materials, new courses, most popular posts, popular books and much more!

https://bridgejunks.com/ https://crownmakesense.com/ https://brithaniabookjudges.com/ https://hughesroyality.com/ https://rhythmholic.com/ https://bandar89.simnasfikpunhas.com/ https://www.100calshop.co.il/products/thailand/ https://myasociados.com/ https://solyser.com/ http://konfidence.cz/ https://muscadinepdx.com/ https://bandar89.parajesandinos.com.ve/ https://goremekoop.com/ https://oncoswisscenter.com/ https://www.turunclifehotel.com/bandar89/ https://www.houseofproducts.biz/ https://taimoormphotography.com/
BIJI18 BIJI18 BIJI18