Share
Explore

Node.js Lesson Workbook: Using ECMAScript 6 (ES6) Module System

Introduction

The ES6 Module system is a modern and efficient way of managing modules in JavaScript.
In this workbook, we'll look at how to use the ES6 Module system in Node.js, with a focus on building a website using Express.js.

Topics Covered:

Understanding Modules in Node.js
ES6 Modules vs CommonJS Modules
Creating and Exporting ES6 Modules
Importing ES6 Modules
Building a Website with Express.js

Lesson 1: Understanding Modules in Node.js

A module in Node.js is a simple or complex functionality organized in single or multiple JavaScript files which can be reused throughout the Node.js application.
Why use modules?
Maintainability: It's easier to maintain functionality oriented modules.
Reusability: Avoid code duplication by reusing modules throughout the application.
Organization: Modules help keep the code organized by functionality or domain.

Lesson 2: ES6 Modules vs CommonJS Modules

In Node.js, modules were originally implemented based on the CommonJS module system.
The require() function is used to import modules, and module.exports or exports is used to export.
ES6 introduced a new module syntax.
With ES6 modules, import is used for importing and export for exporting.
For example, consider a CommonJS module math.js:

// CommonJS
let add = function(x, y) {
return x + y;
}

module.exports = add;

And here's how the same functionality would be implemented using an ES6 module:

// ES6
export const add = (x, y) => x + y;

Lesson 3: Creating and Exporting ES6 Modules

Creating an ES6 module is straightforward. You simply write your functions (or variables, classes, etc.), and then export them using the export keyword. You can export multiple items from the same module.

// calculator.js
export const add = (x, y) => x + y;
export const subtract = (x, y) => x - y;

Lesson 4: Importing ES6 Modules

To use the exported functions, you would import them in another file.

// app.js
import { add, subtract } from './calculator.js';

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


// This error is common when trying to use
// ES6 import/export syntax in Node.js. ES6 modules are
// not fully compatible with Node.js, which uses the CommonJS module syntax.
// However, you can use ES6 module syntax in Node.js by doing the following:

// Change your file extension from .js to .mjs. So your files would look like calculator.mjs and app.mjs.
// Add in your package.json. This tells Node.js to treat .js files as ES6 Modules.

Lesson 5: Building a Website with Express.js (starter for your A1)

Now, let's consider an interesting vertical - a website for a digital library. We'll use Express.js and ES6 modules.
We'll have two modules:
books.js: This module will export an array of books.
server.js: This module will import our books and serve them on a webpage using Express.js.
jsCopy code
// books.js
export const books = [
{ title: '1984', author: 'George Orwell' },
{ title: 'To Kill a Mockingbird', author: 'Harper Lee' },
{ title: 'The Great Gatsby', author: 'F. Scott Fitzgerald' },
];

jsCopy code
// server.js
import express from 'express';
import { books } from './books.js';

const app = express();
const port = 3000;

app.get('/', (req, res) => {
let bookList = books.map(book => `<li>${book.title} by ${book.author}</li>`).join('\n');
res.send(`<ul>${
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.