Bird
Raised Fist0
Node.jsframework~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

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
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.

Practice

(1/5)
1. What does the node command do when you run node script.js in your terminal?
easy
A. It uploads script.js to a web server.
B. It opens the script.js file in a text editor.
C. It compiles script.js into a binary executable.
D. It runs the JavaScript code inside the file script.js using Node.js.

Solution

  1. Step 1: Understand the purpose of the node command

    The node command runs JavaScript files using the Node.js runtime environment.
  2. Step 2: Analyze the command node script.js

    This command tells Node.js to execute the code inside the file named script.js.
  3. Final Answer:

    It runs the JavaScript code inside the file script.js using Node.js. -> Option D
  4. Quick Check:

    Running node file.js executes the file [OK]
Hint: Node runs JavaScript files given as arguments [OK]
Common Mistakes:
  • Thinking node opens files in editors
  • Confusing node with a compiler
  • Assuming node uploads files
2. Which of the following is the correct way to run a JavaScript file named app.js using Node.js?
easy
A. node run app.js
B. node app.js
C. run node app.js
D. execute app.js with node

Solution

  1. Step 1: Recall the correct syntax for running scripts with Node.js

    The correct syntax is node filename.js without extra words.
  2. Step 2: Check each option

    node app.js matches the correct syntax exactly: node app.js.
  3. Final Answer:

    node app.js -> Option B
  4. Quick Check:

    Correct command syntax is node filename.js [OK]
Hint: Use 'node' followed directly by the filename [OK]
Common Mistakes:
  • Adding extra words like 'run' or 'execute'
  • Swapping order of 'node' and filename
  • Using commands not recognized by Node.js
3. Given the file hello.js with this content:
console.log('Hello, Node!');

What will be the output when running node hello.js?
medium
A. Hello, Node!
B. console.log('Hello, Node!');
C. SyntaxError
D. No output

Solution

  1. Step 1: Understand what console.log does

    The console.log function prints the text inside the parentheses to the terminal.
  2. Step 2: Predict the output of running node hello.js

    Running the file executes the code and prints 'Hello, Node!' to the terminal.
  3. Final Answer:

    Hello, Node! -> Option A
  4. Quick Check:

    console.log prints text to terminal [OK]
Hint: console.log prints text when run with node [OK]
Common Mistakes:
  • Expecting code to print the code itself
  • Thinking node throws syntax errors for valid code
  • Assuming no output without browser
4. You try to run node myscript.js but get an error: Error: Cannot find module './myscript.js'. What is the most likely cause?
medium
A. The file myscript.js does not exist in the current directory.
B. Your JavaScript code inside myscript.js has a syntax error.
C. You typed node myscript.js incorrectly; it should be node run myscript.js.
D. Node.js is not installed on your computer.

Solution

  1. Step 1: Understand the error message

    The error says Node.js cannot find the file myscript.js in the current folder.
  2. Step 2: Identify the cause

    This usually means the file is missing or the path is wrong, not that Node.js is missing or the code has syntax errors.
  3. Final Answer:

    The file myscript.js does not exist in the current directory. -> Option A
  4. Quick Check:

    File missing causes 'Cannot find module' error [OK]
Hint: Check file exists in current folder before running [OK]
Common Mistakes:
  • Assuming Node.js is not installed from this error
  • Adding extra words to the node command
  • Confusing file not found with syntax errors
5. You have two files:
index.js:
console.log('Start');
require('./helper.js');
console.log('End');

helper.js:
console.log('Helper loaded');

What will be the output when you run node index.js?
hard
A. Start End Helper loaded
B. Helper loaded Start End
C. Start Helper loaded End
D. SyntaxError

Solution

  1. Step 1: Understand the order of execution in index.js

    The code first prints 'Start', then loads and runs helper.js, then prints 'End'.
  2. Step 2: Trace the output step-by-step

    First, 'Start' is printed. Then helper.js runs and prints 'Helper loaded'. Finally, 'End' is printed.
  3. Final Answer:

    Start Helper loaded End -> Option C
  4. Quick Check:

    require runs the imported file immediately [OK]
Hint: require runs the file immediately in order [OK]
Common Mistakes:
  • Assuming require runs after all code
  • Mixing output order
  • Expecting syntax errors without mistakes