0
0
Node.jsframework~20 mins

Running scripts with node command in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Node.js Script Runner Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:00remaining
What is the output when running this Node.js script?
Consider the following Node.js script saved as script.js. What will be printed to the console when you run node script.js?
Node.js
console.log('Hello from Node.js!');
Aundefined
BHello from Node.js!
CSyntaxError
DReferenceError
Attempts:
2 left
💡 Hint
Think about what console.log does in Node.js.
Predict Output
intermediate
1:30remaining
What happens if you run a script with a syntax error?
Given this Node.js script saved as error.js, what will happen when you run node error.js?
Node.js
console.log('Missing quote);
AReferenceError: Missing quote is not defined
Bundefined
CNo output, script runs silently
DSyntaxError: Invalid or unexpected token
Attempts:
2 left
💡 Hint
Look carefully at the quotes in the code.
component_behavior
advanced
2:00remaining
What will be the output of this asynchronous Node.js script?
Examine the following script saved as async.js. What will be printed when running node async.js?
Node.js
console.log('Start');
setTimeout(() => console.log('Timeout'), 0);
console.log('End');
A
Start
End
Timeout
B
Start
Timeout
End
C
Timeout
Start
End
D
End
Start
Timeout
Attempts:
2 left
💡 Hint
Remember how setTimeout works with zero delay.
🔧 Debug
advanced
2:00remaining
Why does this Node.js script throw an error when run?
Look at this script saved as errorCheck.js. When running node errorCheck.js, it throws an error. What is the cause?
Node.js
const fs = require('fs');
fs.readFile('nonexistent.txt', (err, data) => {
  if (err) throw err;
  console.log(data.toString());
});
AThrows error because require is not defined
BNo error, prints empty string
CThrows error because the file does not exist
DThrows error because readFile is synchronous
Attempts:
2 left
💡 Hint
Check what happens when reading a file that is missing.
📝 Syntax
expert
2:30remaining
Which option correctly runs a Node.js script with ES module syntax?
You have a script module.mjs using ES module syntax with import. How do you run it with Node.js?
Node.js
import fs from 'fs';
console.log('Module loaded');
Anode module.mjs
Bnode --require module.mjs
Cnode --experimental-modules module.mjs
Dnode module.js
Attempts:
2 left
💡 Hint
Modern Node.js supports ES modules with .mjs extension by default.