module.exports is an empty object and a property on the module object. Module  is a special object in NodeJS which contains information about the current module. The exports property is used to expose properties or functions of the current module. Use require to import properties from some module.

// file.js 
module.exports = “Hello world”;


// file2.js 
module.exports = {
   fn: function () { 
// ... 
// app.js 
var message = require(‘./file’);
 console.log(message); // Hello world 

var file2 = require(‘./file2’); console.log(file2.fn());
 // call fn on the file2

With the introduction of ES Modules via ES6, an export and import keyword can be used for exporting and importing properties.

// file.js
 const message = “Hello world”;
 export default message;
// file2.js
 export function fn() { 
// ... 
// app.js
 import message from ‘./file’;
 console.log(message); // Hello world

 import { fn } from ‘./file2’;
 console.log(fn()); // call fn on the file2


“module.exports” Tech Bite was brought to you by Kenan Klisura, Lead Software Engineer at Atlantbh. 

Tech Bites are tips, tricks, snippets or explanations about various programming technologies and paradigms, which can help engineers with their everyday job.

Software DevelopmentTech Bites
February 23, 2024

Background Jobs in Elixir – Oban

When and why do we need background jobs? Nowadays, background job processing is indispensable in the world of web development. The need for background jobs stems from the fact that synchronous execution of time-consuming and resource-intensive tasks would heavily impact an application's  performance and user experience.  Even though Elixir is…
QA/Test AutomationTech Bites
December 22, 2023

Selenium Grid 4 with Docker

Introduction When talking about automation testing, one of the first things that comes to mind is Selenium. Selenium is a free, open-source automated testing framework used to validate web applications across different browsers and platforms. It is not just a single tool but a suite of software. Every component of…

Want to discuss this in relation to your project? Get in touch:

Leave a Reply