What is Node.js?
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. It allows you to run JavaScript on the server-side, outside of a web browser. Before Node.js, JavaScript was primarily used for client-side scripting in browsers.
Node.js was created by Ryan Dahl in 2009 and has since become one of the most popular platforms for building web servers, APIs, real-time applications, and command-line tools.
🚀 Why Node.js?
- ⚡ JavaScript Everywhere: Use the same language for frontend and backend development.
- 🔄 Non-Blocking I/O: Handle thousands of concurrent connections efficiently.
- 📦 NPM Ecosystem: Access to over 2 million packages in the npm registry.
- 🏢 Industry Adoption: Used by Netflix, PayPal, LinkedIn, Uber, and more.
- 🛠️ Versatile: Build web servers, APIs, CLI tools, desktop apps, and IoT applications.
Installing Node.js
Download and install Node.js from the official website or use a version manager like nvm.
Option 1: Direct Download
Visit nodejs.org and download the LTS (Long Term Support) version.
Option 2: Using nvm (Recommended)
# Install nvm (Node Version Manager)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
# Install the latest LTS version
nvm install --lts
# Use a specific version
nvm use 22
# Verify installation
node --version
npm --version
Your First Node.js Program
Create a file called hello.js and add the following code:
// hello.js
console.log('Hello, Node.js!');
// Node.js provides global objects
console.log('Current directory:', __dirname);
console.log('Current file:', __filename);
// Access command line arguments
console.log('Arguments:', process.argv);
// Environment information
console.log('Node version:', process.version);
console.log('Platform:', process.platform);
Run the program from your terminal:
node hello.js
$ node hello.js
Hello, Node.js!
Current directory: /Users/you/projects
Current file: /Users/you/projects/hello.js
Arguments: [ '/usr/local/bin/node', '/Users/you/projects/hello.js' ]
Node version: v20.10.0
Platform: darwin
Node.js vs Browser JavaScript
| Feature | Browser | Node.js |
|---|---|---|
| DOM Access | ✅ Yes | ❌ No |
| File System | ❌ Limited | ✅ Full Access |
| window Object | ✅ Yes | ❌ No (uses global) |
| Network Requests | fetch, XMLHttpRequest | http, https, fetch |
| Module System | ES Modules | CommonJS & ES Modules |
Global Objects in Node.js
Node.js provides several global objects that are available in all modules:
// __dirname - Directory path of current module
console.log(__dirname);
// __filename - File path of current module
console.log(__filename);
// process - Information about the current process
console.log(process.env.NODE_ENV); // Environment variables
console.log(process.cwd()); // Current working directory
process.exit(0); // Exit the process
// global - The global namespace object (like window in browsers)
global.myVariable = 'Hello';
// Buffer - For handling binary data
const buf = Buffer.from('Hello');
// setTimeout, setInterval, setImmediate - Timers
setTimeout(() => console.log('Delayed'), 1000);
💡 Key Takeaways
- • Node.js lets you run JavaScript on the server
- • It's built on Chrome's V8 engine for high performance
- • Use nvm to manage multiple Node.js versions
- • Node.js has different global objects than the browser
- • The npm ecosystem provides millions of packages