Skip to content
Learn Netverks

Lesson

Step 9/36 25% through track

modules-esm

ES modules (ESM)

Last reviewed May 28, 2026 Content v20260528
Track mode
nodejs_server
Means
Node sandbox
Reading
~2 min
Level
beginner

This lesson

This lesson teaches ES modules (ESM): the syntax, APIs, and habits you need before advancing in Node.js.

Module boundaries and npm supply-chain hygiene matter for security reviews and reproducible deploys.

You will apply ES modules (ESM) in contexts like: REST/GraphQL APIs, BFF layers, CLIs, webhooks, and real-time services (with WebSockets).

Run JavaScript on the Node runner when configured—never mix arbitrary shell commands in lessons. Also use ESM import syntax in the editor—the runner executes main.mjs.

When you can explain the previous lesson's ideas without copying starter code.

ESM is the modern standard: import and export with static analysis-friendly syntax. New Node projects set "type": "module" in package.json; this playground runs as ESM (.mjs).

Named and default exports

// math.mjs
export function add(a, b) { return a + b; }
export default function multiply(a, b) { return a * b; }

// app.mjs
import multiply, { add } from './math.mjs';

Built-in imports

import fs from 'node:fs/promises';
import { readFile } from 'node:fs/promises';
import path from 'node:path';

The node: prefix clarifies built-ins and avoids npm name collisions.

Dynamic import

const mod = await import('./feature.mjs');

Loads conditionally—useful for lazy loading or optional dependencies.

Important interview questions and answers

  1. Q: .mjs vs .js with "type":"module"?
    A: Both run as ESM; .mjs forces ESM even without package.json type; .cjs forces CommonJS.
  2. Q: Can you require an ESM module?
    A: Not with sync require—use dynamic import() from CommonJS instead.

Self-check

  1. What does the node: prefix indicate?
  2. When use dynamic import()?

Tip: Use node: prefix for built-ins (import fs from 'node:fs/promises')—clearer and avoids shadowing npm packages named fs.

Interview prep

ESM vs CommonJS default?

New Node projects use "type": "module" in package.json for ESM; .cjs forces CommonJS, .mjs forces ESM regardless of package type.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • import.meta.url for?
  • Default export rule?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump