Relation between CommonJS, AMD and RequireJS?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
CommonJS, AMD, and RequireJS are related, but they are not interchangeable terms. Two of them describe module formats, while one of them is a loader that implements one of those formats.
The clean way to remember the relationship is this: CommonJS is a module specification, AMD is another module specification designed for browsers, and RequireJS is a popular implementation of AMD.
CommonJS and AMD Solve Similar Problems Differently
JavaScript originally had no standard module system, so projects invented conventions for splitting code into reusable files. CommonJS became popular on the server side, especially in Node.js. Its design assumes synchronous loading, which is fine when files are on local disk.
AMD, short for Asynchronous Module Definition, was designed with browsers in mind. Browser scripts may need to fetch files over the network, so asynchronous loading is much more practical.
The core difference is not just syntax. It is the loading model:
- CommonJS typically loads dependencies synchronously
- AMD defines modules so they can be loaded asynchronously
Where RequireJS Fits
RequireJS is not a third module format sitting beside CommonJS and AMD. It is a JavaScript library that loads modules in the browser and follows the AMD format.
That means:
- AMD is the specification
- RequireJS is one implementation of that specification
When you write define([...], function (...) { ... }) in an AMD-style browser project, RequireJS is often the tool interpreting that definition and resolving dependencies.
Here is a small RequireJS setup:
With that in place, RequireJS loads app.js, reads its dependencies, and loads those files asynchronously.
Why These Systems Existed
Each system reflects the environment it was built for.
CommonJS worked well for early server-side JavaScript because reading files synchronously during startup was acceptable. AMD worked better in browsers where blocking the page while several script files download would hurt performance and user experience.
RequireJS became popular because it provided a practical browser loader with dependency management, path configuration, and optimization tooling.
Today, native ES modules cover much of this space, but you will still find CommonJS in Node.js codebases and AMD or RequireJS in older frontend applications and enterprise tools.
How to Think About Them Today
If you are maintaining older code, the most important distinction is conceptual:
- CommonJS tells you how a module exports and imports values
- AMD tells you how a module is defined for asynchronous loading
- RequireJS is the runtime loader that makes AMD modules work in the browser
You may also encounter tools that translate between formats. Bundlers such as Webpack, Rollup, and Vite can often consume CommonJS and AMD inputs and produce modern bundles. That is why many teams no longer author new code directly in AMD even though old modules still run.
Common Pitfalls
- Thinking RequireJS is a competitor to AMD. RequireJS implements AMD rather than replacing it.
- Assuming CommonJS is ideal in the browser. Its synchronous loading model is one reason AMD existed.
- Treating the syntax difference as the whole story. The real difference is when and how dependencies are loaded.
- Ignoring ES modules when starting a new project. For modern code, native
importandexportare usually the better choice.
Summary
- CommonJS and AMD are module definitions, not the same kind of thing as RequireJS.
- CommonJS is associated with synchronous loading and server-side JavaScript.
- AMD was designed for asynchronous browser loading.
- RequireJS is a loader that implements AMD.
- Modern projects usually prefer ES modules, but understanding these older systems is still useful when maintaining legacy code.
Related reading
- Reloading code in a dockerized node.js app with docker-compose
- Remotely debugging my node app that is hosted on AWS
- Remove accents/diacritics in a string in JavaScript
- Remove all child elements of a DOM node in JavaScript
- Remove an onclick listener
- Remove blue border from css custom-styled button in Chrome
- Remove border from IFrame
- Remove duplicate objects from an array using javascript
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.