0
0
Node.jsframework~10 mins

ES Modules import and export in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - ES Modules import and export
Create module file
Export variables/functions
Create main file
Import exported items
Use imported items
Run program and see output
This flow shows how you create a module with exports, then import those exports in another file to use them.
Execution Sample
Node.js
export const greeting = 'Hello';
export function sayHi(name) {
  return `${greeting}, ${name}!`;
}

import { greeting, sayHi } from './module.js';
console.log(sayHi('Alice'));
Exports a constant and a function from one file, imports them in another, then calls the function to print a greeting.
Execution Table
StepActionEvaluationResult
1Export const greetinggreeting = 'Hello'greeting available to import
2Export function sayHisayHi is a functionsayHi available to import
3Import greeting and sayHiImported greeting = 'Hello', sayHi functionImported items ready to use
4Call sayHi('Alice')sayHi('Alice') returns 'Hello, Alice!'Output string ready
5console.log outputPrints 'Hello, Alice!'User sees: Hello, Alice!
💡 Program ends after printing the greeting message.
Variable Tracker
VariableStartAfter ExportAfter ImportAfter Function CallFinal
greetingundefined'Hello''Hello''Hello''Hello'
sayHiundefinedfunctionfunctionfunctionfunction
resultundefinedundefinedundefined'Hello, Alice!''Hello, Alice!'
Key Moments - 3 Insights
Why do we use curly braces {} when importing?
Curly braces are used to import named exports exactly as they were exported. See execution_table step 3 where greeting and sayHi are imported by name.
Can we import without exporting first?
No, you must export variables or functions in the module file before importing them. Step 1 and 2 show exports happen before import in step 3.
What happens if we call sayHi before importing it?
You get an error because sayHi is undefined until imported. The variable_tracker shows sayHi is undefined at start and only defined after import.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of greeting after import?
Anull
Bundefined
C'Hello'
D'Hi'
💡 Hint
Check execution_table step 3 under Evaluation column.
At which step does the program print the greeting message?
AStep 3
BStep 5
CStep 4
DStep 2
💡 Hint
Look for console.log output in execution_table.
If we remove the export keyword from greeting, what happens when importing?
AImport fails because greeting is not exported
BImport still works normally
Cgreeting imports as undefined but no error
DProgram prints 'Hello' anyway
💡 Hint
Refer to key_moments about needing export before import.
Concept Snapshot
ES Modules let you share code between files.
Use 'export' to share variables or functions.
Use 'import {name}' to bring them in.
Curly braces import named exports.
Run Node.js with --experimental-modules or .mjs extension.
This keeps code organized and reusable.
Full Transcript
This lesson shows how ES Modules work in Node.js. First, you create a module file where you export variables or functions using the export keyword. Then, in another file, you import those exports using import with curly braces around the names. After importing, you can use the imported items like normal variables or functions. The example exports a greeting string and a sayHi function, then imports and calls sayHi to print a greeting message. The execution table traces each step from exporting, importing, calling the function, to printing the output. The variable tracker shows how variables change from undefined to defined after export and import. Key moments clarify why curly braces are needed, why export must come before import, and what happens if you call a function before importing it. The visual quiz tests understanding of variable values after import, when output happens, and the importance of export. The snapshot summarizes the syntax and purpose of ES Modules in Node.js.