0
0
Node.jsframework~30 mins

ES Modules import and export in Node.js - Mini Project: Build & Apply

Choose your learning style9 modes available
ES Modules import and export
📖 Scenario: You are building a small Node.js project that uses ES Modules to organize code. You want to split your code into two files: one for utility functions and one for the main program.
🎯 Goal: Create a module file that exports a function, then import and use that function in the main file using ES Modules syntax.
📋 What You'll Learn
Create a file utils.js that exports a function called greet
The greet function takes a name parameter and returns a greeting string
Create a file main.js that imports the greet function from utils.js
Call the greet function in main.js with the argument 'Alice'
💡 Why This Matters
🌍 Real World
Organizing code into modules helps keep projects clean and reusable. ES Modules are the standard way to share code in modern JavaScript and Node.js.
💼 Career
Understanding ES Modules is essential for JavaScript developers working on scalable applications, libraries, or any code that benefits from modular design.
Progress0 / 4 steps
1
Create the utils module with a greet function
Create a file called utils.js and write a function named greet that takes a parameter name and returns the string `Hello, ${name}!`. Export this function using export function greet(name) syntax.
Node.js
Need a hint?

Use export function greet(name) { ... } to export the function directly.

2
Create the main file and import greet
Create a file called main.js. Import the greet function from ./utils.js using import { greet } from './utils.js' syntax.
Node.js
Need a hint?

Use import { greet } from './utils.js'; to import the function.

3
Call the greet function with 'Alice'
In main.js, call the imported greet function with the argument 'Alice' and assign the result to a variable named message.
Node.js
Need a hint?

Use const message = greet('Alice'); to call the function and store the result.

4
Export the message variable from main.js
In main.js, export the variable message using export { message } syntax so it can be used by other modules.
Node.js
Need a hint?

Use export { message }; to export the variable.