0
0
Node.jsframework~20 mins

ES Modules import and export in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ES Modules Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate
2:00remaining
What is the output of this ES module import code?
Consider two files: mathUtils.mjs and app.mjs.

mathUtils.mjs exports a named function square.
In app.mjs, we import square and call it with 4.

What will console.log(result) print?
Node.js
/* mathUtils.mjs */
export function square(x) {
  return x * x;
}

/* app.mjs */
import { square } from './mathUtils.mjs';
const result = square(4);
console.log(result);
Aundefined
B16
CNaN
DError: Cannot find module './mathUtils.mjs'
Attempts:
2 left
💡 Hint
Remember that named exports must be imported with curly braces matching the exported name.
component_behavior
intermediate
2:00remaining
What happens when importing a default export incorrectly?
Given utils.mjs exports a default function:

export default function greet() { return 'Hello'; }

Which import statement in app.mjs will cause a runtime error when calling greet()?
Aimport { greet } from './utils.mjs'; console.log(greet());
Bimport greet from './utils.mjs'; console.log(greet());
Cimport * as greet from './utils.mjs'; console.log(greet());
Dimport { default as greet } from './utils.mjs'; console.log(greet());
Attempts:
2 left
💡 Hint
Default exports are imported without curly braces.
🔧 Debug
advanced
2:00remaining
Why does this ES module import cause a SyntaxError?
Look at this code snippet in app.mjs:

import { add } from './math.mjs'

Running this code causes a SyntaxError. What is the most likely cause?
Node.js
import { add } from './math.mjs';
ARunning the file without the --experimental-modules flag in Node.js 12
BMissing semicolon at the end of the import statement
CImport statements must be at the bottom of the file
DUsing single quotes instead of double quotes in import path
Attempts:
2 left
💡 Hint
Check Node.js version and module support.
state_output
advanced
2:00remaining
What is the value of variable 'count' after these imports and mutations?
In counter.mjs we have:

export let count = 0;
export function increment() { count++; }


In app.mjs:

import { count, increment } from './counter.mjs';
increment();
console.log(count);


What will be logged?
AError: Assignment to constant variable
Bundefined
C0
D1
Attempts:
2 left
💡 Hint
ES module imports are live bindings: mutations to the exported variable from within the module are visible to importers.
🧠 Conceptual
expert
3:00remaining
Which option correctly re-exports a named export with a new name?
You want to re-export a named export foo from moduleA.mjs as bar in moduleB.mjs.

Which code in moduleB.mjs achieves this?
Aexport * as bar from './moduleA.mjs';
Bimport { foo as bar } from './moduleA.mjs'; export { foo };
Cexport { foo as bar } from './moduleA.mjs';
Dexport { bar } from './moduleA.mjs';
Attempts:
2 left
💡 Hint
Use export with alias syntax.