module.exports

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.

Protractor parallel execution
QA/Test AutomationTech Bites
May 12, 2023

Protractor parallel execution

Why Parallel Testing? When designing automation test suites, the ability to run tests in parallel is a key feature because of the following benefits: Execution time - Increasing tests efficiency and, finally, faster releases Device compatibility - There are many devices with various versions and specifications that need to be…
Introduction to GraphQL
QA/Test AutomationTech Bites
May 11, 2023

Introduction to GraphQL

In today's Tech Bite topic, we will get familiar with GraphQL and explain its growth in popularity in recent years as the new standard for working with APIs. What is GraphQL? GraphQL stands for Graph Query Language. It is an API specification standard and, as its name says - a…
IQR in Automation Testing
QA/Test AutomationTech Bites
April 25, 2023

IQR in Automation Testing: Unleashing the Data Analytics Potential

Usually, when people talk about statistics, they think only about numbers, but it is much more.  Statistics is an irreplaceable tool in different fields like data analysis, predictive modeling, quality control, scientific research, decision-making models, etc. Overall, statistics provide a valuable toolset for understanding, describing, and making predictions about data,…

Leave a Reply