Skip to main content
Node.js is the foundation of every topic in this course. Before writing any API code, students need to understand what Node.js is, how it runs, and how to organize a project. This page explains the runtime, the package system, and the essential tools to build backend applications.

What Is Node.js?

Node.js is a JavaScript runtime environment built on Chrome’s V8 engine. It allows JavaScript code to run on a server (outside the browser). Before Node.js, JavaScript could only run in browsers.

Key characteristics

Non-blocking I/O is what makes Node.js excellent for APIs and web servers. It handles many concurrent requests efficiently without spawning extra threads.

The Event Loop (Simplified)

The event loop is Node.js’s mechanism for handling asynchronous operations:
  1. Call Stack — executes synchronous code line by line
  2. Node APIs — handles async operations (fs, http, timers)
  3. Callback Queue — queues completed async callbacks
  4. Event Loop — moves callbacks from the queue to the call stack when it is empty
Even with a 0ms timeout, the callback runs last because it goes through the event loop. This is fundamental to understanding asynchronous Node.js behavior.

npm: Node Package Manager

npm is the default package manager for Node.js. It lets you install, manage, and share reusable code packages from the npm registry.

Essential npm commands

Understanding package.json

package.json is the project manifest. Every Node.js project has one.

nodemon for Development

nodemon watches your files and restarts the server automatically when code changes. Essential for development.

require() vs import/export

Node.js supports two module systems. You must know both.
To use ES Modules, add to package.json:
You cannot mix require() and import in the same file. Choose one module system per project.

Your First HTTP Server (Built-in http module)

Before using Express, understand how Node’s built-in http module works. This is what Express wraps.
Run it:

Your First Express Server

Express makes building servers much easier:

.gitignore: What to Exclude

Always create a .gitignore before your first commit:
Never commit node_modules (huge and platform-specific) or .env (contains secrets). These are the two most critical entries in .gitignore.

Key Terms to Memorize

Common Mistakes and Exam Tips

When you clone a repo, node_modules is not included. Always run npm install first.
This causes a syntax error. CommonJS uses require/module.exports. ES Modules uses import/export. Pick one per project.
Restarting the server manually after every code change is slow. Add nodemon as a dev dependency and use npm run dev.
If port 3000 is already in use, change the port or kill the existing process. Use process.env.PORT || 3000 to read from environment variables.
Exam tip: Know the difference between dependencies and devDependencies in package.json. A common question asks which category a package like Jest (testing) or nodemon (dev server) belongs in. Both are devDependencies.