CodingNic

Node.js Fundamentals

Exercises

Node.js Fundamentals 25 min read

Exercises

Objectives

By the end of this lesson, you should be able to:

  • Predict event loop execution order correctly
  • Use process and environment variables in a real script
  • Create a project with npm and write a CommonJS module

⚠️ A note on verification: every command and output in this lesson was actually run with Node.js.

Exercise 1: Event Loop Ordering

a) Before running anything, predict the output order of this script:

javascript
console.log("A");

setTimeout(() => console.log("D"), 0);

Promise.resolve().then(() => console.log("C"));

process.nextTick(() => console.log("B"));

console.log("A2");

b) Run it and check your prediction. Expected result:

text
A
A2
B
C
D

c) Explain, in your own words, why B (nextTick) runs before C (a promise callback), and why D (setTimeout) runs last of all.

d) Rewrite the script so that “D” logs before “C”, without removing or reordering any of the four callback registrations, only changing what’s inside them (hint: think about what controls a macrotask’s priority relative to microtasks, and consider whether this is possible at all, explain your conclusion either way).

Exercise 2: process and Global Objects

a) Write a script that logs process.env.APP_NAME, falling back to "DefaultApp" if it isn’t set.

b) Extend the script to also log process.argv.slice(2) (every argument after the script path itself).

c) Run it as APP_NAME=MyBackend node script.js one two. Expected output:

text
MyBackend
[ 'one', 'two' ]

d) Run it again without setting APP_NAME, and confirm it falls back to "DefaultApp".

Exercise 3: npm and CommonJS Modules

a) Create a new folder and run npm init -y, confirming a package.json file is created.

b) Create a file greetings.js exporting two functions, sayHello(name) and sayGoodbye(name), each returning a formatted greeting string, using module.exports.

c) Create a main.js that require()s both functions and calls each with the name "Maya". Expected output:

text
Hello, Maya!
Goodbye, Maya!

d) Convert the same two files to ES Modules instead: add "type": "module" to package.json, change module.exports to individual export statements, and change require() to import. Confirm the output is identical.

Exercise 4: Environment Variables

a) Create a .env file with two variables of your choice (for example, API_URL and TIMEOUT_MS).

b) Install dotenv, and write a script that loads the .env file and logs both variables.

c) Add .env to a .gitignore file, and create a .env.example file with the same variable names but placeholder values instead of real ones.

d) Explain, in your own words, why a teammate cloning this project would need their own .env file, and what the .env.example file is for.

Recap

This module covered what Node.js actually is under the hood: its architecture and the V8 engine, the event loop’s execution order, global objects, project management with npm, both module systems, environment variables, and debugging tools beyond console.log.

Next module: Node’s own core built-in modules, fs, path, http, and more, no framework required yet.