Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is the main purpose of ES Modules in Node.js?
ES Modules allow you to organize and reuse code by importing and exporting functions, objects, or values between files in a clear and standardized way.
Click to reveal answer
beginner
How do you export a single function as the default export in an ES Module?
Use export default functionName; to export a single function as the default export from a module.
Click to reveal answer
beginner
What is the syntax to import a default export from another module?
Use <code>import anyName from './module.js';</code> where <code>anyName</code> is your chosen name for the default export.
Click to reveal answer
intermediate
How do you export multiple named values from a module?
Use <code>export const name1 = value1;</code> or <code>export { name1, name2 };</code> to export multiple named values.
Click to reveal answer
intermediate
How can you import multiple named exports from a module?
Use <code>import { name1, name2 } from './module.js';</code> to import specific named exports.
Click to reveal answer
Which keyword is used to export a default value in ES Modules?
Arequire
Bexport const
Cmodule.exports
Dexport default
✗ Incorrect
The export default keyword exports a single default value from a module.
How do you import a named export called calculate from a module?
Aimport { calculate } from './module.js';
Bimport calculate from './module.js';
Cconst calculate = require('./module.js');
Dexport { calculate } from './module.js';
✗ Incorrect
Named exports are imported using curly braces: import { calculate } from './module.js';.
What file extension should you use for ES Modules in Node.js by default?
A.mjs
B.cjs
C.js
D.json
✗ Incorrect
The .mjs extension explicitly marks a file as an ES Module in Node.js.
Which statement is true about mixing CommonJS and ES Modules in Node.js?
AYou can freely mix <code>require</code> and <code>import</code> in the same file.
BES Modules can import CommonJS modules, but CommonJS cannot import ES Modules directly.
CCommonJS modules are the same as ES Modules.
DYou must rename all files to .cjs to use ES Modules.
✗ Incorrect
ES Modules can import CommonJS modules, but CommonJS modules cannot directly import ES Modules without special handling.
How do you export a variable named count as a named export?
Aexport default count;
Bimport count from './module.js';
Cexport { count };
Dmodule.exports = count;
✗ Incorrect
Use export { count }; to export count as a named export.
Explain how to export and import both default and named values using ES Modules in Node.js.
Think about how you share one main thing versus multiple things from a file.
You got /4 concepts.
Describe the difference between ES Modules and CommonJS modules in Node.js and how they interact.
Consider the keywords and compatibility between the two module systems.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using export and import in Node.js ES Modules?
easy
A. To create new variables inside a file
B. To run JavaScript code faster in Node.js
C. To convert JavaScript code into another language
D. To share code between different files by exporting and importing parts
Solution
Step 1: Understand the role of export
The export keyword allows parts of a file (like functions or variables) to be shared with other files.
Step 2: Understand the role of import
The import keyword 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 D
Quick Check:
Export and import = share code [OK]
Hint: Export shares code, import uses it elsewhere [OK]
Common Mistakes:
Thinking export/import changes code speed
Confusing export with variable creation
Believing export/import converts languages
2. Which of the following is the correct syntax to export a function named greet in an ES Module file?
easy
A. export function greet() {}
B. module.exports = greet;
C. export = greet;
D. exports.greet = function() {}
Solution
Step 1: Identify ES Module export syntax
In ES Modules, use export 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 A
Quick Check:
ES Modules export function = export function [OK]
Hint: Use 'export function' for ES Modules exports [OK]
Common Mistakes:
Using CommonJS syntax in ES Modules
Writing invalid export syntax
Confusing export default with named export
3. Given the following files, what will be logged when running 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));
medium
A. 10
B. 5
C. undefined
D. SyntaxError
Solution
Step 1: Understand the imports and exports
The file utils.js exports a constant value = 5 and a function double that multiplies input by 2.
Step 2: Trace the code in main.js
It imports value and double, then calls double(value) which is double(5), returning 10.
Final Answer:
10 -> Option A
Quick Check:
double(5) = 10 [OK]
Hint: Import exports correctly, then call functions with values [OK]
Common Mistakes:
Forgetting to add .js extension in import
Confusing export default with named exports
Expecting value instead of double(value)
4. What is the error in the following code snippet?
// math.js export function add(a, b) { return a + b; }
// app.js import { add } from './math'; console.log(add(2, 3));
medium
A. Function add is not exported correctly
B. Cannot import named exports with curly braces
C. Missing file extension in import statement
D. Using CommonJS syntax in ES Modules
Solution
Step 1: Check import statement syntax
The import statement uses import { add } from './math'; but misses the .js extension required in Node.js ES Modules.
Step 2: Confirm export syntax is correct
The function add is correctly exported with export function add(), so no error there.
Final Answer:
Missing file extension in import statement -> Option C
Quick Check:
Node.js ES Modules need file extensions [OK]
Hint: Always include .js extension in ES Module imports [OK]