What is Node.js?
Node.js is a powerful JavaScript runtime built on Chrome’s V8 JavaScript engine. It allows you to run JavaScript on the server side, enabling full-stack JavaScript development.
Key Concepts
Event-Driven Architecture
Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.
graph TD
A[Client Request] --> B[Event Loop]
B --> C{Is Operation Async?}
C -->|Yes| D[Delegate to Thread Pool]
C -->|No| E[Execute Immediately]
D --> F[Callback Queue]
F --> B
E --> G[Send Response]
Single-Threaded Event Loop
Despite being single-threaded for JavaScript execution, Node.js can handle many concurrent connections efficiently through its event loop mechanism.
Why Node.js?
- Fast Performance: V8 engine compiles JavaScript to native machine code
- NPM Ecosystem: Largest package registry with millions of reusable modules
- Scalability: Non-blocking I/O perfect for I/O-intensive applications
- Full-Stack JavaScript: Same language for frontend and backend
Installation
macOS/Linux
# Using nvm (recommended)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
nvm install --lts
Verify Installation
node --version
npm --version
Your First Node.js Program
Create a file hello.js:
console.log('Hello, Node.js!');
console.log('Node version:', process.version);
console.log('Platform:', process.platform);
Run it:
node hello.js
What JavaScript engine does Node.js use?
What makes Node.js efficient for I/O operations?
Which package manager comes with Node.js by default?
Next Steps
In the next lesson, we’ll explore Node.js modules and the CommonJS module system.