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.
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.| 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 |
There are two common ways to use ES Modules in Node.js.
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.
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.
The export keyword allows a module to share its
functions, variables, and classes with other modules.
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.
The import keyword lets us use values exported
by another module.
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.
We can export multiple functions or variables from the same file.
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 {
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
A module can provide one default export. When importing it, you can choose the local name without using curly braces.
export default function greet(name) {
return 'Hello, ' + name + '!';
}
Create app.js:
import greet from './message.js';
console.log(greet('Amit'));
Output:
Hello, Amit!
You can give a named import a different local name by using
the as keyword.
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
Follow these steps to run your program:
package.json file with "type": "module".math.js and export the add() function.app.js and import the function.node app.js
import and export. In Node.js,
make sure your files are configured as ES Modules before
running the examples.
calculator.js.add() function that adds two numbers.multiply() function that multiplies two numbers.app.js and import both functions.Expected output:
8
15
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?