Menu

HTML & JS Topic

Introduction to Node.js

What is Node.js?

Node.js is an open-source, cross-platform JavaScript runtime environment that allows developers to execute JavaScript code outside of a web browser. It was created by Ryan Dahl in 2009 and is built on Chrome's V8 JavaScript engine.

Key characteristics:

  • Not a programming language - It's a runtime environment for executing JavaScript

  • Not a framework - It's a platform that provides the foundation for building applications

  • Event-driven architecture - Uses non-blocking I/O operations

  • Single-threaded but highly scalable through asynchronous operations

Why Use Node.js?

Advantages:

  1. Fast & Efficient - Non-blocking I/O makes it excellent for I/O-intensive operations

  2. Full-stack JavaScript - Use JavaScript on both client and server

  3. Large Ecosystem - npm (Node Package Manager) has the world's largest open-source library collection

  4. High Scalability - Event-driven architecture handles many concurrent connections

  5. Real-time Applications - Perfect for chat apps, gaming servers, live notifications

  6. Microservices Friendly - Lightweight and modular

Common Use Cases:

  • Web servers and APIs (REST, GraphQL)

  • Real-time applications (chat, collaboration tools)

  • Streaming applications

  • Command-line tools

  • Backend for single-page applications

  • Microservices architecture

Node.js vs Browser JavaScript

Aspect Node.js Browser JavaScript
Environment Server-side runtime Client-side browser
Global Object global window
DOM Access No DOM access Full DOM access
File System Can read/write files Cannot access file system directly
Modules CommonJS/ES Modules ES Modules
Window/Document Not available Available
require() Available Not available (unless bundled)
Purpose Server applications Interactive web pages

Installing Node.js & npm

Step-by-Step Installation:

1. Download Node.js:

  • Visit nodejs.org

  • Download the LTS (Long Term Support) version for stability

2. Installation:

  • Windows: Run the .msi installer, follow prompts

  • Mac: Use the .pkg installer or brew install node

  • Linux: Use package manager:

    bash

    # Ubuntu/Debian
    sudo apt-get install nodejs npm
    
    # or using NodeSource repository
    curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
    sudo apt-get install -y nodejs

     

3. Verify Installation:

bash

node --version  # Should show version like v18.x.x
npm --version   # Should show version like 9.x.x
npx --version   # npx comes with npm 5.2+

4. Alternative Installation Methods:

  • nvm (Node Version Manager) - Manage multiple Node versions:

    bash

    # Install nvm
    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
    
    # Install Node.js using nvm
    nvm install --lts
    nvm use --lts

     

  • Docker: Run Node.js in containers

Your First Node.js Program:

  1. Create a file app.js:

javascript

// app.js
console.log("Hello from Node.js!");

// Use built-in modules
const os = require('os');
console.log(`System: ${os.platform()}`);
  1. Run it:

bash

node app.js

What is npm?

npm (Node Package Manager) is:

  • Default package manager for Node.js

  • World's largest software registry

  • Used to install, share, and manage dependencies

  • Comes bundled with Node.js installation

Basic npm commands:

bash

npm init                 # Create package.json
npm install <package>    # Install a package
npm install --save-dev   # Install as dev dependency
npm update               # Update packages
npm run <script>         # Run scripts from package.json

Getting Started Example:

javascript

// Simple HTTP server
const http = require('http');

const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Hello from Node.js Server!');
});

server.listen(3000, () => {
  console.log('Server running at http://localhost:3000/');
});

Node.js has revolutionized backend development by enabling JavaScript to be used full-stack, making development more consistent and efficient across the entire application stack.

Comments (0)

No comments yet. Be the first to comment!

LecturreAI Assistant

Hello! I'm LecturreAI

What would you like to learn today?