Lesson 9 of 50 – ES Modules
18% Completed
18%

Node.js ES Modules

ES Modules (ESM) are a JavaScript module system that uses import and export. They help us divide a program into separate files and reuse code in different parts of an application.

In simple words: ES Modules allow one JavaScript file to export functions or variables and another file to import and use them.

1. What Are ES Modules?

ES Modules are part of modern JavaScript. Node.js supports ES Modules, allowing developers to organize applications into reusable files.

The two main keywords are:

  • export – makes functions, variables, or classes available to other modules.
  • import – brings exported values into another module.
CommonJS vs ES Modules
Feature CommonJS ES Modules
Load a module require() import
Export a value module.exports export
Typical file extensions .cjs .mjs
JavaScript file configuration Can use .cjs or a CommonJS package configuration Can use .mjs or "type": "module" in package.json

2. How to Enable ES Modules in Node.js

There are two common ways to use ES Modules in Node.js.

Method 1: Use package.json

Create a file named package.json:

{
    "name": "esm-demo",
    "version": "1.0.0",
    "type": "module"
}

The setting "type": "module" tells Node.js to interpret ordinary .js files in this package scope as ES Modules.

Method 2: Use the .mjs Extension

You can also save your ES Module code in a file with the .mjs extension.

app.mjs

Node.js treats the .mjs extension as an ES Module.

3. Using export

The export keyword allows a module to share its functions, variables, and classes with other modules.

Example: Export a Function

Create math.js:

export function add(a, b) {
    return a + b;
}

The add() function is now a named export that another ES Module can import.

4. Using import

The import keyword lets us use values exported by another module.

Example: Import a Function

Create app.js:

import { add } from './math.js';

console.log(add(10, 20));

Output:

30

Notice that we use curly braces around add because it is a named export. The relative import includes the .js extension.

5. Exporting Multiple Values

We can export multiple functions or variables from the same file.

Example: calculator.js
export function add(a, b) {
    return a + b;
}

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

export function multiply(a, b) {
    return a * b;
}

export const calculatorName = 'Simple Calculator';
Import Multiple Values
import {
    add,
    subtract,
    multiply,
    calculatorName
} from './calculator.js';

console.log(add(10, 5));
console.log(subtract(10, 5));
console.log(multiply(10, 5));
console.log(calculatorName);

Output:

15
5
50
Simple Calculator

6. Default Export

A module can provide one default export. When importing it, you can choose the local name without using curly braces.

Example: message.js
export default function greet(name) {
    return 'Hello, ' + name + '!';
}
Import the Default Export

Create app.js:

import greet from './message.js';

console.log(greet('Amit'));

Output:

Hello, Amit!
Remember: Named exports are imported using their exported names inside curly braces. Default exports can be imported using a chosen local name without curly braces.

7. Renaming an Imported Function

You can give a named import a different local name by using the as keyword.

Example

Suppose math.js contains:

export function add(a, b) {
    return a + b;
}

In app.js:

import { add as sum } from './math.js';

console.log(sum(4, 6));

Output:

10

8. Running an ES Module Program

Follow these steps to run your program:

  1. Install Node.js.
  2. Create a project folder.
  3. Create a package.json file with "type": "module".
  4. Create math.js and export the add() function.
  5. Create app.js and import the function.
  6. Open a terminal in the project folder.
  7. Run the program.
node app.js
Key point: ES Modules use import and export. In Node.js, make sure your files are configured as ES Modules before running the examples.

9. Practical Assignment

Practice Task
Create a small calculator using ES Modules.
  1. Create a file named calculator.js.
  2. Create an add() function that adds two numbers.
  3. Create a multiply() function that multiplies two numbers.
  4. Export both functions using named exports.
  5. Create app.js and import both functions.
  6. Print the results of adding 5 and 3 and multiplying 5 and 3.

Expected output:

8
15

🧠 Quiz – ES Modules

Select the correct answer for each question.

1. Which keyword is used to import a named export?



2. Which keyword makes a function available to other ES Modules?



3. Which package.json setting enables ES Modules for .js files in its package scope?