Discover how ES Modules turn messy code copying into neat, shareable building blocks!
Why ES Modules import and export in Node.js? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have many JavaScript files, and you want to use functions from one file inside another by copying and pasting all the code manually.
Manually copying code is messy, leads to mistakes, and makes it hard to update or reuse parts of your program. It's like rewriting the same recipe every time you want to cook.
ES Modules let you neatly export parts of your code and import them where needed. This keeps your code organized, easy to maintain, and reusable, just like sharing a recipe book instead of rewriting recipes.
function greet() { console.log('Hello'); } // copy-paste this in every fileexport function greet() { console.log('Hello'); } // import { greet } from './greet.js';It enables clean code sharing across files, making your projects scalable and easier to understand.
Think of a cooking show where each chef shares their secret sauce recipe separately, so others can use it without guessing or copying by hand.
Manual code sharing is error-prone and hard to maintain.
ES Modules provide a simple way to export and import code parts.
This leads to cleaner, reusable, and organized code.
Practice
export and import in Node.js ES Modules?Solution
Step 1: Understand the role of
Theexportexportkeyword allows parts of a file (like functions or variables) to be shared with other files.Step 2: Understand the role of
Theimportimportkeyword is used to bring those shared parts into another file to use them.Final Answer:
To share code between different files by exporting and importing parts -> Option DQuick Check:
Export and import = share code [OK]
- Thinking export/import changes code speed
- Confusing export with variable creation
- Believing export/import converts languages
greet in an ES Module file?Solution
Step 1: Identify ES Module export syntax
In ES Modules, useexport function functionName() {}to export a function.Step 2: Check other options for CommonJS syntax
Options B and D use CommonJS style, not ES Modules. export = greet; is invalid syntax.Final Answer:
export function greet() {} -> Option AQuick Check:
ES Modules export function = export function [OK]
- Using CommonJS syntax in ES Modules
- Writing invalid export syntax
- Confusing export default with named export
node main.js?// utils.js
export const value = 5;
export function double(x) { return x * 2; }// main.js
import { value, double } from './utils.js';
console.log(double(value));Solution
Step 1: Understand the imports and exports
The fileutils.jsexports a constantvalue = 5and a functiondoublethat multiplies input by 2.Step 2: Trace the code in
It importsmain.jsvalueanddouble, then callsdouble(value)which isdouble(5), returning 10.Final Answer:
10 -> Option AQuick Check:
double(5) = 10 [OK]
- Forgetting to add .js extension in import
- Confusing export default with named exports
- Expecting value instead of double(value)
// math.js
export function add(a, b) { return a + b; }
// app.js
import { add } from './math';
console.log(add(2, 3));Solution
Step 1: Check import statement syntax
The import statement usesimport { add } from './math';but misses the.jsextension required in Node.js ES Modules.Step 2: Confirm export syntax is correct
The functionaddis correctly exported withexport function add(), so no error there.Final Answer:
Missing file extension in import statement -> Option CQuick Check:
Node.js ES Modules need file extensions [OK]
- Omitting file extensions in import paths
- Confusing CommonJS and ES Module syntax
- Assuming named exports don't use curly braces
// data.js
const secret = 'hidden';
export const visible = 'shown';
export default function getSecret() { return secret; }// index.js
import getSecret, { visible } from './data.js';
console.log(visible);
console.log(getSecret());What will be the output when running
node index.js?Solution
Step 1: Understand default and named exports
getSecretis the default export function returningsecret.visibleis a named export with value 'shown'.Step 2: Analyze import and console.log calls
import getSecret, { visible }correctly imports default and named exports. Loggingvisibleprints 'shown'. CallinggetSecret()returns 'hidden'.Final Answer:
shown
hidden -> Option BQuick Check:
Default + named import works = shown, hidden [OK]
- Mixing default and named imports incorrectly
- Expecting secret variable to be exported directly
- Confusing order of import syntax
