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:- Call Stack — executes synchronous code line by line
- Node APIs — handles async operations (fs, http, timers)
- Callback Queue — queues completed async callbacks
- 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.package.json:
Your First HTTP Server (Built-in http module)
Before using Express, understand how Node’s built-inhttp module works. This is what Express wraps.
Your First Express Server
Express makes building servers much easier:.gitignore: What to Exclude
Always create a.gitignore before your first commit:
Key Terms to Memorize
Common Mistakes and Exam Tips
Forgetting npm install after cloning
Forgetting npm install after cloning
When you clone a repo,
node_modules is not included. Always run npm install first.Using require and import in the same file
Using require and import in the same file
This causes a syntax error. CommonJS uses
require/module.exports. ES Modules uses import/export. Pick one per project.Not using nodemon in development
Not using nodemon in development
Restarting the server manually after every code change is slow. Add nodemon as a dev dependency and use
npm run dev.Wrong PORT causing EADDRINUSE errors
Wrong PORT causing EADDRINUSE errors
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.