Introduction
ES Modules, also called ESM, are the modern JavaScript module system supported by Node.js. They use import and export instead of the CommonJS require() and module.exports syntax. ES Modules make it easier to organize reusable JavaScript code into separate files. In this chapter, you will practice named exports, default exports, multiple imports, aliases, and combining different modules in simple Node.js applications. Node.js ES Modules practice questions with solutions help to build concepts.
Question 1: How do you enable ES Modules in a Node.js project?
Problem
Configure a Node.js project so that it can use import and export.
Solution
First create a Node.js project:
mkdir my-esm-app
cd my-esm-app
npm init -y
Open package.json and add:
{
"name": "my-esm-app",
"version": "1.0.0",
"type": "module"
}
Now create app.js:
const message = "Hello ES Modules!";
console.log(message);
Run:
node app.js
Output
Hello ES Modules!
Step-by-Step Explanation
- Create a Node.js project.
- Run
npm init -y. - Open
package.json. - Add
"type": "module". - Node.js will treat
.jsfiles as ES Modules. - You can now use
importandexportsyntax in your JavaScript files.
The "type": "module" setting is an important part of using ESM with .js files.
Question 2: How do you create a named export in an ES Module?
Problem
Create a module that exports a function using named export syntax.
Solution
Create greeting.js:
export function greet(name) {
return "Hello, " + name + "!";
}
Create app.js:
import { greet } from "./greeting.js";
console.log(greet("Riya"));
Run:
node app.js
Output
Hello, Riya!
Step-by-Step Explanation
- Create
greeting.js. - Create the
greet()function. - Add
exportbefore the function. - This makes
greetavailable to other modules. - In
app.js, useimport. { greet }means you are importing the named export.- The
.jsextension is included in the relative import. - Call
greet("Riya").
Named exports are useful when a module contains several reusable functions or values.
Question 3: How do you export a variable using ES Modules?
Problem
Create a module that exports a student’s name and age.
Solution
Create student.js:
export const name = "Aman";
export const age = 19;
Create app.js:
import { name, age } from "./student.js";
console.log("Name:", name);
console.log("Age:", age);
Output
Name: Aman
Age: 19
Step-by-Step Explanation
- Create a
namevariable usingconst. - Add the
exportkeyword. - Create an
agevariable. - Export it as well.
- Import both variables using
{ name, age }. - Use the variables inside
app.js.
You can export variables, functions, classes, and other supported JavaScript values.
Question 4: How do you create a default export in ES Modules?
Problem
Create a module that exports one greeting function as its default export.
Solution
Create greeting.js:
function greet(name) {
return "Welcome, " + name;
}
export default greet;
Create app.js:
import greet from "./greeting.js";
console.log(greet("Aarav"));
Run:
node app.js
Output
Welcome, Aarav
Step-by-Step Explanation
- Create the
greet()function. - Use
export default. - This makes
greetthe default export of the module. - Import the default export without curly braces.
- You can choose the local name used during import.
- Call the function.
A module can have one default export.
Question 5: What is the difference between named and default imports?
Problem
Create one module with a named export and a default export, then import both.
Solution
Create tools.js:
export const appName = "Student App";
export function add(a, b) {
return a + b;
}
export default function greet() {
return "Hello from the app!";
}
Create app.js:
import greet, { appName, add } from "./tools.js";
console.log(appName);
console.log(add(10, 20));
console.log(greet());
Output
Student App
30
Hello from the app!
Step-by-Step Explanation
appNameis a named export.add()is another named export.greet()is the default export.- Default imports do not use curly braces.
- Named imports use curly braces.
- All three values can be imported in the same statement.
The syntax makes the difference easy to identify:
import defaultValue, { namedValue } from "./file.js";
Question 6: How do you export multiple functions from an ES Module?
Problem
Create a calculator module containing addition, subtraction, and multiplication functions.
Solution
Create 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;
}
Create app.js:
import {
add,
subtract,
multiply
} from "./calculator.js";
console.log(add(20, 10));
console.log(subtract(20, 10));
console.log(multiply(20, 10));
Output
30
10
200
Step-by-Step Explanation
- Create three functions.
- Add
exportto each function. - These become named exports.
- Import the required functions inside
app.js. - Use curly braces around named imports.
- Call each function with two numbers.
ES Modules make it simple to keep related functions in their own files.
Question 7: How do you rename an imported ES Module value?
Problem
Import a function with a different local name.
Solution
Create greeting.js:
export function greet(name) {
return "Hello, " + name;
}
Create app.js:
import { greet as sayHello } from "./greeting.js";
console.log(sayHello("Rahul"));
Output
Hello, Rahul
Step-by-Step Explanation
- The module exports a function called
greet. - In
app.js, useas. greet as sayHellogives the imported function a new local name.- The original exported name remains
greet. - Inside
app.js, you usesayHello().
This is useful when two different modules export values with the same name.
Question 8: How do you import all named exports from an ES Module?
Problem
Import all named exports from a calculator module under one object name.
Solution
Create calculator.js:
export function add(a, b) {
return a + b;
}
export function multiply(a, b) {
return a * b;
}
Create app.js:
import * as calculator from "./calculator.js";
console.log(calculator.add(5, 10));
console.log(calculator.multiply(5, 10));
Output
15
50
Step-by-Step Explanation
- The calculator module exports two functions.
import * as calculatorimports all named exports.- The imported values are available through the
calculatorobject. calculator.add()calls the addition function.calculator.multiply()calls the multiplication function.
This approach can be useful when you want to keep several related exports under one namespace.
Question 9: How do you export and import a JavaScript class using ES Modules?
Problem
Create a Student class in one module and use it in another file.
Solution
Create student.js:
export default class Student {
constructor(name, age) {
this.name = name;
this.age = age;
}
introduce() {
return `My name is ${this.name} and I am ${this.age} years old.`;
}
}
Create app.js:
import Student from "./student.js";
const student = new Student("Riya", 18);
console.log(student.introduce());
Output
My name is Riya and I am 18 years old.
Step-by-Step Explanation
- Create a
Studentclass. - Add a constructor for
nameandage. - Add an
introduce()method. - Export the class using
export default. - Import it using
import Student. - Create a new object using
new Student(). - Call the
introduce()method.
ES Modules can export classes just like functions and variables.
Question 10: How do you build a small Node.js application using ES Modules?
Problem
Create a simple student result application using separate ES Modules.
Solution
Create student.js:
export const student = {
name: "Aarav",
marks: 85
};
Create result.js:
export function checkResult(marks) {
if (marks >= 40) {
return "Pass";
}
return "Fail";
}
Create app.js:
import { student } from "./student.js";
import { checkResult } from "./result.js";
const result = checkResult(student.marks);
console.log("Student:", student.name);
console.log("Marks:", student.marks);
console.log("Result:", result);
Run:
node app.js
Output
Student: Aarav
Marks: 85
Result: Pass
Step-by-Step Explanation
student.jsstores the student’s information.- The
studentobject is exported. result.jscontains thecheckResult()function.- The function is exported as a named export.
app.jsimports both modules.- The student’s marks are passed to
checkResult(). - The function checks whether the marks are at least
40. - The final result is displayed.
This example combines named exports, imports, objects, functions, and multiple ES Modules in one small application.
Key Takeaways
- ES Modules are the modern JavaScript module system supported by Node.js.
- ES Modules use
importandexport. "type": "module"can be added topackage.jsonto use ESM syntax in.jsfiles.- Named exports use the
exportkeyword. - Named imports normally use curly braces.
- Default exports use
export default. - Default imports do not require curly braces.
- You can export functions, variables, objects, and classes.
ascan rename an imported value.import * ascan import all named exports under one object.- ES Modules help keep Node.js applications organized and reusable.
- Relative ES Module imports commonly include the
.jsfile extension.
FAQs
1. What are ES Modules in Node.js?
ES Modules, or ESM, are a modern JavaScript module system that allows code to be divided into reusable files. They use import and export syntax.
2. How do I enable ES Modules in Node.js?
For .js files, add this property to your package.json:
{
"type": "module"
}
After that, Node.js treats .js files in that package as ES Modules.
3. What is the difference between CommonJS and ES Modules?
CommonJS generally uses:
const math = require("./math");
and:
module.exports = math;
ES Modules use:
import math from "./math.js";
and:
export default math;
They are two different module systems supported by Node.js.
4. What is a named export in ES Modules?
A named export is a value exported with a specific name.
export function add(a, b) {
return a + b;
}
It can then be imported using:
import { add } from "./calculator.js";
5. What is a default export?
A default export represents the main exported value of a module.
export default function greet() {
return "Hello!";
}
It can be imported without curly braces:
import greet from "./greeting.js";
6. Do ES Module imports need the .js extension?
For relative file imports in Node.js ESM, you should normally include the file extension.
For example:
import { add } from "./calculator.js";
7. Can I export classes using ES Modules?
Yes. Classes can be exported using either named exports or default exports.
For example:
export default class Student {
// class code
}
You can then import the class into another ES Module.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
