Introduction
CommonJS Modules are the traditional module system used in Node.js. Before the introduction of ES Modules (ESM), CommonJS was the standard way to organize JavaScript code into reusable files.
A CommonJS module uses:
module.exportsto export code from a file.require()to import code into another file.
Every JavaScript file in Node.js is treated as a separate module. This helps developers split applications into smaller, reusable components such as utility functions, configuration files, API services, database connections, and business logic.
Although modern projects increasingly use ES Modules, CommonJS is still widely used in many existing Node.js applications and libraries. Understanding CommonJS is essential because you will often encounter it in legacy codebases and popular npm packages.
For automation engineers, CommonJS modules help organize Playwright, Selenium, Cypress, and API automation projects by separating utilities, page objects, configuration files, and reusable functions.
In this tutorial, you’ll learn how CommonJS modules work and how to use them effectively.
What are CommonJS Modules?
A CommonJS module is a JavaScript file that exports values using module.exports and imports them using require().
Each file has its own private scope, helping avoid variable conflicts and improving code organization.
Why Use CommonJS Modules?
CommonJS modules help developers:
Organize code into separate files.
Reuse functionality.
Improve maintainability.
Reduce duplicate code.
Keep applications modular.
Simplify testing.
Manage large projects efficiently.
Basic Syntax
Export
module.exports = value;
Import
const variable =
require("./file");
Exporting a Function
math.js
function add(a, b) {
return a + b;
}
module.exports =
add;
Importing a Function
app.js
const add =
require("./math");
console.log(
add(10, 20)
);
Sample Output
30
Exporting Multiple Functions
calculator.js
function add(a, b) {
return a + b;
}
function multiply(a, b) {
return a * b;
}
module.exports = {
add,
multiply
};
Importing Multiple Functions
const calculator =
require("./calculator");
console.log(
calculator.add(5, 5)
);
console.log(
calculator.multiply(5, 5)
);
Sample Output
10
25
Exporting an Object
config.js
module.exports = {
appName:
"Automation Framework",
version:
"1.0"
};
Importing an Object
const config =
require("./config");
console.log(
config.appName
);
console.log(
config.version
);
Sample Output
Automation Framework
1.0
Exporting a Class
user.js
class User {
login() {
console.log(
"User logged in."
);
}
}
module.exports =
User;
Importing a Class
const User =
require("./user");
const user =
new User();
user.login();
Sample Output
User logged in.
Automation Testing Examples
CommonJS modules help organize automation frameworks into reusable components.
Playwright Example
browser.js
function launchBrowser() {
console.log(
"Launching Playwright browser."
);
}
module.exports =
launchBrowser;
test.js
const launchBrowser =
require("./browser");
launchBrowser();
Sample Output
Launching Playwright browser.
Selenium Example
login.js
function login() {
console.log(
"User logged in."
);
}
module.exports =
login;
test.js
const login =
require("./login");
login();
Sample Output
User logged in.
Cypress Example
dashboard.js
function verifyDashboard() {
console.log(
"Dashboard verified."
);
}
module.exports =
verifyDashboard;
test.js
const verifyDashboard =
require("./dashboard");
verifyDashboard();
Sample Output
Dashboard verified.
API Testing Example
api.js
function callApi() {
console.log(
"API request executed."
);
}
module.exports =
callApi;
test.js
const callApi =
require("./api");
callApi();
Sample Output
API request executed.
Data-Driven Testing Example
data.js
module.exports =
[
"TC001",
"TC002",
"TC003"
];
test.js
const testCases =
require("./data");
console.log(testCases);
Sample Output
[ 'TC001', 'TC002', 'TC003' ]
Real-World Uses of CommonJS Modules
CommonJS modules are commonly used for:
Utility functions.
Configuration files.
Database connections.
API services.
Authentication.
Logging utilities.
Validation helpers.
Page Object Models.
Test data management.
Report generation.
CommonJS vs ES Modules
| Feature | CommonJS | ES Modules |
|---|---|---|
| Import | require() | import |
| Export | module.exports | export |
| Loading | Synchronous | Static and asynchronous-friendly |
| Browser Support | No | Yes |
| Standard | Node.js | ECMAScript |
| File Type | .js | .js or .mjs |
Common Mistakes
Forgetting module.exports
If you do not export a value, it cannot be accessed by other modules.
Incorrect File Path
Provide the correct relative path when using require().
Example:
const math =
require("./math");
Mixing CommonJS and ES Modules Incorrectly
Avoid using require() and import together in the same file unless you understand Node.js interoperability.
Best Practices
Keep each module focused on one responsibility.
Use meaningful file names.
Export only what is necessary.
Group related functions together.
Organize automation utilities into separate modules.
Avoid circular dependencies.
Prefer ES Modules for new projects, but understand CommonJS for existing applications.
Conclusion
CommonJS modules have been the foundation of Node.js development for many years and continue to power countless applications and libraries. By using module.exports and require(), developers can create modular, reusable, and maintainable applications.
For automation engineers, CommonJS modules provide a practical way to organize browser utilities, API helpers, configuration files, page objects, and test data. Although ES Modules are now the preferred standard for modern JavaScript, understanding CommonJS remains essential because many existing Node.js projects and npm packages still rely on it.
Frequently Asked Questions (FAQs)
What is a CommonJS module?
A CommonJS module is a JavaScript file that exports functionality using module.exports and imports it using require().
What is module.exports?
module.exports is used to make variables, functions, objects, or classes available to other files.
What does require() do?
require() imports code that has been exported from another CommonJS module.
Can I export multiple functions?
Yes. You can export an object containing multiple functions and access them after importing.
Why are CommonJS modules useful in automation testing?
They help organize automation projects by separating reusable utilities, page objects, API helpers, configuration files, and test data into modular files.
Key Takeaways
CommonJS is the traditional module system used in Node.js.
It uses
module.exportsto export code.It uses
require()to import code.Each file is treated as an independent module.
Modules improve code organization and reusability.
You can export functions, objects, arrays, variables, and classes.
CommonJS is widely used in legacy Node.js projects and many npm packages.
ES Modules are recommended for new projects, but CommonJS knowledge is still essential.
CommonJS helps organize automation frameworks into reusable components.
Mastering CommonJS modules makes it easier to work with existing Node.js applications and libraries.
