ajcwebdev
Blog post cover art for Notes on ES Modules with Chantastic

Notes on ES Modules with Chantastic

Published:

A collection of examples from Michael Chan (Chantastic) demonstrating static and dynamic imports, export lists, and import aliases.

Outline

Introduction

ES Modules (JavaScript Modules, if you like) help us structure, organize and isolate code. These examples focus on the ESM features available in a Node environment such as dynamic modules, export lists, and module aliasing.

Most JS apps in early 2021 are still bundled before being sent to the browser. Features covered here are common to the latest LTS Node environments for most popular Node bundling tools like Webpack, Rollup, and esbuild.

Index Main File

  • This is where all exercises are run
  • Examples in readme.md can be copy/pasted into index.js

Prepared Module Files

  • The names relate to the type of content inside
  • Various examples might use these prepared modules to explore a concept

Assignments

  • ./modules/assignment.mjs is an empty module.

Import a Module from the File System

Modules are imported using the import keyword and a string path to that module. This is predominantly done at the opening of a file. We can import any module that exists on the file system. Import the assignment.mjs module using the import keyword and file path:

index.js
import "./modules/assignment.mjs"

Leaky Modules

The first thing to know about modules is that they leak. They don’t perfectly encapsulate all code. Global code is global code, even if in a module. This might sounds bad (and it can be) but it’s an important feature.

Add Globally Executing Code to a Module

Globally executing code can be console.log("booo!!") or an assignment like global.leak = "oh no!".

modules/assignment.mjs
console.log("sah dude")
global.leak = "oops"
index.js
import "./modules/assignment.mjs"
console.log(global.leak)
sah dude
oops

Import a Module Dynamically

The import keyword has two variants: static and dynamic. You can change a static import to a dynamic import by adding parenthesis around the path string.

import "./modules/assignment.mjs" // static
import("./modules/assignment.mjs") // dynamic

Change the Static Import to a Dynamic Import

A dynamic import returns a Promise, so try handling that promise using await.

index.js
await import("./modules/assignment.mjs")
console.log(global.leak)

Or use .then() to resolve the Promise.

index.js
import("./modules/assignment.mjs")
.then(() => console.log(global.leak))
sah dude
oops

Export Lists

Global code is executed at import but variables and function declarations are not. Even though a function or variable might exist in an imported module, it can’t be accessed outside of that module.

Add a Variable and Function Declaration to the Module

modules/assignment.mjs
let fallbackName = "there"
function greet(name = fallbackName) {
return `Hey, ${name}!`
}

Use an Export List to Export a Function

We can export anything defined in our module by adding it to the comma separated export list.

modules/assignment.mjs
let fallbackName = "there"
function greet(name = fallbackName) {
return `Hey, ${name}!`
}
export { fallbackName, greet }

Anything exported can also use the same list syntax, {}, for import. Importing specific imports from a module requires the from keyword before the path string.

index.js
import { greet } from "./modules/assignment.mjs"
console.log(greet())
Hey, there!

export list is commonly at the end of a file to guarantee that everything exported — or referenced — already exists. Now with an export list, export and import just the greet function.

modules/assignment.mjs
let fallbackName = "dude"
function greet(name = fallbackName) {
return `Sah, ${name}`
}
export { greet }

greet still has access to fallbackName, even though fallbackName isn’t exported.

index.js
import { greet } from "./modules/assignment.mjs"
console.log(greet())
Sah, dude

Alias or Rename Imports and Exports

Modules might not share object syntax with modules but they still allow for aliasing (or renaming) of variables and functions using the as keyword.

modules/assignment.mjs
export { aGoodLocalName as aBetterExportedName }

It works identically on both the import and export sides.

index.js
import {
aBetterExportedName as anEvenBetterContextualName
} from "./modules/assignment"

Rename a Function while Exporting

At import, use the new function name you’ve exported and then rename it back to greet at import.

modules/assignment.mjs
let fallbackName = "dude";
function greet(name = fallbackName) {
return `Sah, ${name}`
}
export { greet as sahDude }
index.js
import { sahDude as greet } from "./modules/assignment.mjs"
console.log(greet())
Sah, dude