HOMEAUTHORSBUSINESS
ES Modules In Node.js

ES Modules In Node.js

By Sameer
Published in NodeJS
September 05, 2023
2 min read

Since its inception, JavaScript has evolved, and with it, its module systems. ES Modules (ESM) represents the latest standard, promising to unify the way we handle modules in JavaScript. With Node.js introducing built-in support for ES Modules, it’s high time we explore this feature and understand its implications for our projects.

Introduction

ES Modules (often referred to as ESM) is the official standard format to package JavaScript code for reuse. It introduces the use of ’import’ and ’export’ statements, a departure from Node.js’s traditional CommonJS (CJS) module system which used ’require()’ and ’module.exports‘.

Key Features of ES Modules

  1. Static Analysis: Imports and exports are determined at compile-time, not runtime. This static nature allows for better tooling and tree shaking (eliminating dead code).
  2. Async and Dynamic Imports: ES Modules support both synchronous and dynamic imports, providing more flexibility.
  3. Named and Default Exports: ESM allows for multiple named exports and a default export, enabling more structured and readable import statements.

Using ES Modules in Node.js

Setup:

Starting from Node.js version 13.2.0, support for ES Modules became stable. Here’s how you can use it:

  1. Either name your file with the ’.mjs’ extension.
  2. Or set ’“type”: “module”’ in your ’package.json‘.

Code Examples:

1. Exporting from a Module

Named Exports:

// utils.mjs
export function add(a, b) {
  return a + b
}

export function subtract(a, b) {
  return a - b
}

Default Export:

// greet.mjs
export default function greet(name) {
  return `Hello, ${name}!`
}

2. Importing in Another Module

Import Named Exports:

// app.mjs
import { add, subtract } from './utils.mjs'

console.log(add(5, 3)) // Outputs: 8
console.log(subtract(5, 3)) // Outputs: 2

Import Default Export:

// app.mjs
import greet from './greet.mjs'

console.log(greet('Alice')) // Outputs: Hello, Alice!

3. Dynamic Imports

Dynamic imports return promises, making them perfect for conditional loading of modules.

// dynamic.mjs
if (someCondition) {
  import('./moduleToLoad.mjs').then(module => {
    // Use the imported module
  })
}

Migrating from CommonJS

When transitioning to ES Modules from CommonJS, be aware:

  1. Default Imports: CJS doesn’t have a true “default” export. When importing a default export from an ESM file in CJS, it will be under the ’default’ property.
  2. File Extensions: Unlike ’require()’, ES ’import’ statements require file extensions.

Conclusion

The evolution of JavaScript and its module systems has reached a significant milestone with the introduction of ES Modules (ESM) in Node.js. This new standard not only simplifies the way developers handle and structure their code but also provides enhanced features like static analysis and dynamic imports. While the transition from the traditional CommonJS to ESM might seem daunting, the benefits of better tooling, improved tree shaking, and structured exports make it a worthy endeavor. It’s crucial, however, for developers to remain mindful of the nuances, such as the necessity for file extensions in ‘import’ statements and the treatment of default exports when migrating. As the JavaScript ecosystem continues to grow and mature, embracing ESM in Node.js will likely prove essential for modern development practices.


Sameer

Sameer

Front-end Developer

Expertise

react

Social Media

instagramtwitterwebsite

Related Posts

Deno vs Node.js
NodeJS
Deno vs Node.js
September 05, 2023
1 min
© 2023, All Rights Reserved.

Quick Links

About UsContact Us

Social Media