Lab 01: Hello World & Node.js Basics

🎯 Objective

Write and run your first JavaScript program in Node.js, explore the runtime environment, and understand how JavaScript executes in a server-side context.

πŸ“š Background

JavaScript was originally a browser language, but Node.js (released 2009) brought it to the server using the V8 engine. Node.js is event-driven and non-blocking β€” perfect for I/O-heavy applications. In this lab you'll use the innozverse-js:latest Docker image which ships Node.js v20 LTS.

⏱️ Estimated Time

45–60 minutes

πŸ“‹ Prerequisites

  • Docker installed and running

  • Basic terminal/shell familiarity

πŸ› οΈ Tools Used

  • Docker (innozverse-js:latest)

  • Node.js v20

πŸ”¬ Lab Instructions

Step 1: Verify the Environment

πŸ“Έ Verified Output:

πŸ’‘ The -e flag runs a string of JavaScript inline. --rm removes the container after it exits.


Step 2: Hello World

πŸ“Έ Verified Output:

πŸ’‘ console.log() is the primary output function in Node.js. It writes to stdout with a newline.


Step 3: Explore the Process Object

πŸ“Έ Verified Output:

πŸ’‘ process is a global object in Node.js containing runtime information. No import required.


Step 4: Multiple Console Methods

πŸ“Έ Verified Output:

πŸ’‘ console.error and console.warn write to stderr, useful for logging errors separately from normal output.


Step 5: Write and Run a Script File

Create a file named hello.js on your host machine:

πŸ“Έ Verified Output:

πŸ’‘ -v /tmp:/tmp mounts your host /tmp directory into the container, so Node.js can read your file.


Step 6: Pass Command-Line Arguments

πŸ“Έ Verified Output:

πŸ’‘ process.argv[0] is node, process.argv[1] is the script path. Your arguments start at index 2.


Step 7: Comments and Basic Operators

πŸ“Έ Verified Output:

πŸ’‘ JavaScript uses IEEE 754 floating-point math. 10 / 3 gives a repeating decimal, not an integer.


Step 8: String Output Formatting

πŸ“Έ Verified Output:

πŸ’‘ JSON.stringify(obj, null, 2) pretty-prints JSON with 2-space indentation β€” very handy for debugging.


βœ… Verification

Run this final snippet to confirm all basics work:

Expected Output:

🚨 Common Mistakes

Mistake
Fix

console.log without parentheses

Always use console.log(...) with parens

Forgetting semicolons

JS has ASI but explicit semicolons avoid ambiguity

Mixing ' and "

Both work β€” pick one style and be consistent

process.argv[0] for your arg

User args start at index 2

File not found in Docker

Use -v to mount your host directory

πŸ“ Summary

  • Node.js runs JavaScript outside the browser using the V8 engine

  • console.log() is your primary output tool (also .error, .warn, .info)

  • process is a global object with runtime info

  • Run scripts with node filename.js or inline with node -e "code"

  • Use -v to mount files into Docker containers

πŸ”— Further Reading

Last updated