DEV Community

Spartacus
Spartacus

Posted on

ECMAScript and CommonJs

Most of us doesn't know or forget the difference between ECMA Script and CommonJs. So my role is to simply and key to update in your mind about the concepts.

Image description

1. What is ECMAScript (ES)?
ECMAScript (ES) is the standardized language specification that defines the core features, syntax, and behavior of JavaScript.
Updates to ECMAScript introduce new features and standards that modern JavaScript adheres to (e.g., ES6 introduced ES modules, let, const, arrow functions, etc.).

2. What is CommonJS?
CommonJS is a module system developed to bring modularity to JavaScript. It was designed to enable modular code on environments like Node.js before the advent of ES modules.
In CommonJS:
Modules are loaded using require().
Code and values are shared between modules using module.exports.

3. What are ES Modules?
ES Modules (ESM) are the standardized way of handling modules in JavaScript. Introduced in ES6 (2015), they allow modular development in both the browser and Node.js environments.
In ES modules:
Modules are imported using the import keyword.
Functionality is exported using export.


4. Key Differences Between ES Modules and CommonJS

  • A. Syntax
    ES Modules use modern syntax (import/export) designed for readability and consistency.
    CommonJS uses older require() and module.exports.

  • B. Loading and Execution
    ES Modules are asynchronous:

When using import, ES modules are loaded asynchronously by the runtime.
Allows better performance and non-blocking operations.
Example:

import { func } from './module.js';
console.log('Imported ES module asynchronously');
Enter fullscreen mode Exit fullscreen mode

CommonJS is synchronous:
When you use require(), the module is loaded and executed synchronously.
Suitable for Node.js environments but may block the event loop if the module is large or slow.
Example:

const func = require('./module');
console.log('Loaded CommonJS module synchronously');
Enter fullscreen mode Exit fullscreen mode
  • C. Scope and Structure Static vs Dynamic:

ES Modules are static:
Imports and exports must be declared at the top level of the file.
Module structure is determined at compile time.
Example:

// Valid
import { func } from './module.js';
export const value = 42;

// Invalid: dynamic imports are not allowed here
if (condition) {
    import { otherFunc } from './otherModule.js';
}
Enter fullscreen mode Exit fullscreen mode

CommonJS is dynamic:
You can call require() anywhere in the code.
Module structure is determined at runtime.
Example:

if (condition) {
    const func = require('./module');
}
Enter fullscreen mode Exit fullscreen mode
  • D. Browser Support ES Modules: Fully supported in modern browsers without the need for transpilation. Import/export works directly in . Example: html</li> </ul> <script type="module"> import { func } from './module.js'; func();

    CommonJS:
    Does not work in browsers natively. Requires bundlers like Webpack or Browserify to transpile CommonJS modules into a format compatible with browsers

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay