Node.js ES Modules Practice Questions with Solutions

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

  1. Create a Node.js project.
  2. Run npm init -y.
  3. Open package.json.
  4. Add "type": "module".
  5. Node.js will treat .js files as ES Modules.
  6. You can now use import and export syntax 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

  1. Create greeting.js.
  2. Create the greet() function.
  3. Add export before the function.
  4. This makes greet available to other modules.
  5. In app.js, use import.
  6. { greet } means you are importing the named export.
  7. The .js extension is included in the relative import.
  8. 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

  1. Create a name variable using const.
  2. Add the export keyword.
  3. Create an age variable.
  4. Export it as well.
  5. Import both variables using { name, age }.
  6. 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

  1. Create the greet() function.
  2. Use export default.
  3. This makes greet the default export of the module.
  4. Import the default export without curly braces.
  5. You can choose the local name used during import.
  6. 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

  1. appName is a named export.
  2. add() is another named export.
  3. greet() is the default export.
  4. Default imports do not use curly braces.
  5. Named imports use curly braces.
  6. 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

  1. Create three functions.
  2. Add export to each function.
  3. These become named exports.
  4. Import the required functions inside app.js.
  5. Use curly braces around named imports.
  6. 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

  1. The module exports a function called greet.
  2. In app.js, use as.
  3. greet as sayHello gives the imported function a new local name.
  4. The original exported name remains greet.
  5. Inside app.js, you use sayHello().

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

  1. The calculator module exports two functions.
  2. import * as calculator imports all named exports.
  3. The imported values are available through the calculator object.
  4. calculator.add() calls the addition function.
  5. 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

  1. Create a Student class.
  2. Add a constructor for name and age.
  3. Add an introduce() method.
  4. Export the class using export default.
  5. Import it using import Student.
  6. Create a new object using new Student().
  7. 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

  1. student.js stores the student’s information.
  2. The student object is exported.
  3. result.js contains the checkResult() function.
  4. The function is exported as a named export.
  5. app.js imports both modules.
  6. The student’s marks are passed to checkResult().
  7. The function checks whether the marks are at least 40.
  8. 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 import and export.
  • "type": "module" can be added to package.json to use ESM syntax in .js files.
  • Named exports use the export keyword.
  • 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.
  • as can rename an imported value.
  • import * as can import all named exports under one object.
  • ES Modules help keep Node.js applications organized and reusable.
  • Relative ES Module imports commonly include the .js file 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.

Scroll to Top