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
Docker (innozverse-js:latest)
π¬ 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.
πΈ Verified Output:
π‘ JavaScript uses IEEE 754 floating-point math. 10 / 3 gives a repeating decimal, not an integer.
πΈ 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
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