Why modules? JavaScript has evolved significantly over the years, and one of the critical areas of advancement is in how we manage and structure code using modules. Modules allow developers to break down complex applications into smaller, manageable parts, each with a specific purpose. There are several module systems that have been adopted over time, […]

The origins of the CommonJS module system CommonJS modules have been a foundational part of JavaScript, especially in Node.js, since before ES modules (ESM) were officially adopted. Designed to enable modular and maintainable code, CommonJS allows developers to import and export functionality across files using require and module.exports. This approach was crucial in shaping the […]

ESM, or ECMAScript Modules, is the official module system introduced in ECMAScript 6 (ES6), also known as ECMAScript 2015. ESM provides a native, standardized way to organize and structure code in JavaScript applications. Unlike CommonJS, which is synchronous and primarily used in Node.js, ESM is designed to work in both browser and server-side environments. Asynchronous […]

In the world of JavaScript, modularity is key to writing clean, maintainable, and scalable code. Two popular module formats that have emerged to facilitate this are AMD (Asynchronous Module Definition) and UMD (Universal Module Definition). These formats help developers manage dependencies and organize code in a modular fashion, making it easier to maintain large codebases. […]

Let’s dive into how module systems behave at build-time vs runtime, and the distinctions between CommonJS (CJS), ECMAScript Modules (ESM), and tools like TypeScript and webpack. I’ll go step by step and also address when module resolution can fail. 1. CommonJS (CJS) — runtime-driven 2. ECMAScript Modules (ESM) — static & runtime 3. TypeScript compilation […]

You can import modules relatively or Non-relatively: How does TypeScript find modules that you are referencing? Either via Classic or Node –moduleResolution strategy. Classic is the default when emitting AMD, System or ES2015 modules. Node is the default when emitting CommonJS or UMD modules. Resolving Classic Relative Imports: Resolving Classic Non-relative Imports: Resolving Node Relative […]