module.exports node js code example

Example 1: node js module export class

module.exports = class Person {
	constructor(firstName, lastName) {
		this.firstName = firstName;
		this.lastName = lastName;
	}

	display() {
		console.log(this.firstName + " " + this.lastName);
	}
}

Example 2: What is the syntax to export a function from a module in Node.js

function foo() {}
function bar() {}

// To export above functions:
module.exports = foo;
module.exports = bar;

// And in the file you want to use these functions,
// import them like this:
const foo = require('./module/path');
const bar = require('./module/path');

Example 3: Node require module

// Use require.main.require to get a module from the root folder.
const Globals = require.main.require("./globals.js");
const YourCustomModule = require("./yourmodule.js");

console.log(YourCustomModule.message);

// --- yourmodule.js ---
module.exports = {
	message: "Hello World!",
	otherData: "Hello Grepper!"
};

Example 4: how to export module in node js

module.exports ={
 //functions
}

Example 5: module.exports equivalent es6

exports["default"] = A; module.exports = exports["default"];