Working with Classic Modules

Working with Classic Modules (JavaScript)

In the world of JavaScript, modules help split code into smaller, reusable parts. Before the introduction of ES6 modules (import/export), developers relied on Classic Modules like:

  • CommonJS

  • AMD

  • UMD

These are still used today in many environments like Node.jsolder projects, and legacy browsers.

Let’s learn how to work with classic modules.


What Are Classic Modules?

Classic modules are systems created before JavaScript had built-in module support.

They help:

  • Organize code

  • Reuse logic

  • Load only what you need

3 Most Common Classic Module Systems:

  1. CommonJS – used in Node.js

  2. AMD (Asynchronous Module Definition) – used in browsers

  3. UMD (Universal Module Definition) – works in both environments


 1. CommonJS (Node.js)

CommonJS is the standard module format for Node.js.

Example File: math.js

function add(a, b) { return a + b; } function subtract(a, b) { return a - b; } // Export the functions module.exports = { add, subtract };

Using the Module: main.js

const math = require('./math'); console.log(math.add(5, 3)); // 8 console.log(math.subtract(10, 4)); // 6

CommonJS loads synchronously. Great for servers, not for browsers.


2. AMD (Asynchronous Module Definition)

AMD was designed for browsers to load modules asynchronously.

Popularized by tools like RequireJS.

Define a Module:

// math.js define([], function() { return { add: function(a, b) { return a + b; }, subtract: function(a, b) { return a - b; } }; });

Use the Module:

require(['math'], function(math) { console.log(math.add(2, 3)); // 5 });

AMD works well in the browser — loads files in parallel.


 3. UMD (Universal Module Definition)

UMD is a hybrid that works in both Node.js and browsers.

 Example:

(function (root, factory) { if (typeof define === 'function' && define.amd) { // AMD define([], factory); } else if (typeof module === 'object' && module.exports) { // CommonJS module.exports = factory(); } else { // Browser global root.myModule = factory(); } }(typeof self !== 'undefined' ? self : this, function () { function greet(name) { return 'Hello, ' + name; } return { greet }; }));

This module works in all environments: AMD, CommonJS, and browser global.


Why Use Classic Modules?

Even today, you may use classic modules if:

  • Working with Node.js projects

  • Using older JavaScript codebases

  • Building code that must run in legacy browsers

  • Working with libraries written in CommonJS or AMD

  • Using bundlers like Webpack (which still support these)


Tools That Use Classic Modules

ToolUses Which Module Type?
Node.jsCommonJS
RequireJSAMD
WebpackSupports all types
BabelCan convert ES6 to CommonJS
BrowserifyConverts CommonJS for browser use

Export Patterns in Classic Modules

Single Function Export

module.exports = function greet(name) { return 'Hello, ' + name; };

Multiple Named Exports

module.exports = { greet, sayBye };

Conditional Export

if (isBrowser) { define([], function() { return myModule; }); } else { module.exports = myModule; }

Importing in Classic Modules

CommonJS:

const greet = require('./greet');

AMD:

require(['greet'], function(greet) { greet('Alice'); });

Module Directory Structure

Organize classic modules like this:

project/ lib/ math.js logger.js main.js

Use relative paths for CommonJS (require('./lib/math')) and module names for AMD.


Tips When Working with Classic Modules

TipWhy It Helps
Stick to one module systemAvoid confusion in legacy projects
Use UMD for wide compatibilityWorks in Node + browsers
Bundle with Webpack or BrowserifyMakes CommonJS usable in browsers
Use exports shorthandCleaner syntax in CommonJS
Document dependenciesHelps future devs understand structure

Classic Modules vs ES Modules

FeatureClassic ModulesES Modules (ES6+)
Syntaxrequiremodule.exportsimportexport
Supported inNode.js, old browsersModern browsers & Node
Load TypeSync (CJS) / Async (AMD)Always async
ToolingWebpack, RequireJSNative in browsers (modern)
Tree ShakingNoYes (in bundlers)

Migrating to ES Modules (Optional)

Many teams are now moving from CommonJS to ES Modules.

CommonJS:

const greet = require('./greet');

ES Module:

import greet from './greet.js';

To enable ES Modules in Node.js, use:

  • "type": "module" in package.json

  • File extensions .mjs or .js


Summary

Classic ModuleWhere It's UsedSyntax Example
CommonJSNode.jsrequire() / module.exports
AMDBrowsers (via RequireJS)define() / require()
UMDUniversalWorks in CJS + AMD + global


Comments

Popular posts from this blog

Tosca System Requirements and Installation Guide (Step-by-Step)

How to Install Selenium for Python Step-by-Step

Tosca Commander: A Beginner’s Overview