Bird
Raised Fist0
Javascriptprogramming~10 mins

Running JavaScript using Node.js - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print 'Hello, Node.js!' to the console.

Javascript
console.[1]('Hello, Node.js!');
Drag options to blanks, or click blank then click option'
Alog
Bprint
Cwrite
Decho
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'print' or 'echo' which are not valid console methods in JavaScript.
Trying to use 'write' which is not a console method.
2fill in blank
medium

Complete the command to run a JavaScript file named 'app.js' using Node.js.

Javascript
node [1]
Drag options to blanks, or click blank then click option'
Aapp.js
Brun
Cstart.js
Dindex.js
Attempts:
3 left
💡 Hint
Common Mistakes
Typing 'run' after node which is not needed.
Using a different file name than the one given.
3fill in blank
hard

Fix the error in the code to correctly import the 'fs' module in Node.js.

Javascript
const fs = require([1]);
Drag options to blanks, or click blank then click option'
A'file-system'
B'fs'
C'filesystem'
D'fs-module'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect module names like 'file-system' or 'filesystem'.
Forgetting to put the module name inside quotes.
4fill in blank
hard

Fill both blanks to create a simple HTTP server using Node.js.

Javascript
const http = require('http');
const server = http.[1]((req, res) => {
  res.[2]('Hello World');
});
Drag options to blanks, or click blank then click option'
AcreateServer
Bsend
Cwrite
Dlisten
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'listen' instead of 'createServer' for the first blank.
Using 'send' which is not a method on the response object in Node.js.
5fill in blank
hard

Fill all three blanks to start the server listening on port 3000 and log a message.

Javascript
server.[1](3000, () => {
  console.[2]('Server is running on port ' + [3]);
});
Drag options to blanks, or click blank then click option'
Alisten
Blog
C3000
Dstart
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'start' instead of 'listen' to start the server.
Using 'print' or other console methods instead of 'log'.
Not using the port number variable or value correctly.

Practice

(1/5)
1. What does the command node filename.js do?
easy
A. Runs the JavaScript code inside filename.js using Node.js
B. Opens the file filename.js in a text editor
C. Compiles filename.js into a different programming language
D. Deletes the file filename.js from your computer

Solution

  1. Step 1: Understand the command node filename.js

    This command tells Node.js to run the JavaScript code inside the file named filename.js.
  2. Step 2: Identify what Node.js does

    Node.js executes JavaScript code outside the browser, so it runs the code in the file.
  3. Final Answer:

    Runs the JavaScript code inside filename.js using Node.js -> Option A
  4. Quick Check:

    node filename.js runs code = C [OK]
Hint: Node runs JavaScript files given after the command [OK]
Common Mistakes:
  • Thinking it opens the file in an editor
  • Confusing running with compiling
  • Assuming it deletes the file
2. Which of the following is the correct way to start an interactive Node.js session in the terminal?
easy
A. run node
B. node filename.js
C. node
D. start node.js

Solution

  1. Step 1: Recall how to start Node.js interactively

    Typing node alone in the terminal starts an interactive JavaScript session.
  2. Step 2: Check other options

    node filename.js runs a file, not interactive mode; run node and start node.js are invalid commands.
  3. Final Answer:

    node -> Option C
  4. Quick Check:

    Just node starts interactive mode [OK]
Hint: Just type 'node' to enter interactive mode [OK]
Common Mistakes:
  • Adding extra words like 'run' or 'start'
  • Confusing running a file with interactive mode
  • Using file names when none are needed
3. What will be the output when running this file with node test.js?
console.log('Hello from Node.js!');
medium
A. SyntaxError
B. Hello from Node.js!
C. undefined
D. No output

Solution

  1. Step 1: Understand the code inside test.js

    The code uses console.log to print a string to the console.
  2. Step 2: Know what Node.js does when running the file

    Node.js executes the code and prints the string to the terminal.
  3. Final Answer:

    Hello from Node.js! -> Option B
  4. Quick Check:

    console.log prints text = Hello from Node.js! [OK]
Hint: console.log prints output to terminal [OK]
Common Mistakes:
  • Expecting no output
  • Confusing console.log with return value
  • Thinking it causes an error
4. You try to run node app.js but get an error: SyntaxError: Unexpected token. What is the most likely cause?
medium
A. You typed node incorrectly
B. You forgot to save app.js before running
C. Node.js is not installed on your computer
D. The app.js file has invalid JavaScript syntax

Solution

  1. Step 1: Understand the error message

    A SyntaxError: Unexpected token means the JavaScript code has a syntax mistake.
  2. Step 2: Check other options

    Typing node wrong usually gives a different error; missing Node.js causes 'command not found'; unsaved file won't run but usually no syntax error.
  3. Final Answer:

    The app.js file has invalid JavaScript syntax -> Option D
  4. Quick Check:

    SyntaxError means code syntax is wrong [OK]
Hint: SyntaxError means code inside file is wrong [OK]
Common Mistakes:
  • Assuming Node.js is missing
  • Thinking the command was typed wrong
  • Not saving file before running
5. You want to quickly test a small JavaScript expression without creating a file. What is the best way to do this using Node.js?
hard
A. Run node in terminal to open interactive mode and type the expression
B. Create a file, write the code, then run node filename.js
C. Use npm start to run the expression
D. Open a browser and run the code in the console

Solution

  1. Step 1: Identify how to run code without a file

    Running node alone opens an interactive session where you can type JavaScript directly.
  2. Step 2: Compare other options

    Creating a file is slower; npm start runs scripts defined in package.json, not quick tests; browser console is outside Node.js environment.
  3. Final Answer:

    Run node in terminal to open interactive mode and type the expression -> Option A
  4. Quick Check:

    Interactive mode = quick testing [OK]
Hint: Use just 'node' to test code quickly without files [OK]
Common Mistakes:
  • Thinking npm start runs any code
  • Believing browser console is Node.js
  • Always creating files for small tests
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard