Introduction to Node.js & Setup

🌿 Node.js Lesson 1 Beginner

Node.js is an open-source, cross-platform JavaScript runtime built on Chrome's V8 JavaScript engine. It allows you to run JavaScript code on the server side, outside the browser, enabling full-stack development with a single language.

1 What Makes Node.js Different?

Node.js uses a non-blocking, event-driven I/O model that makes it highly efficient for data-intensive, real-time applications:

  • Single-threaded Event Loop: Node handles thousands of concurrent connections using a single thread rather than spawning a new thread per request, saving memory overhead.
  • V8 Engine: Compiles JavaScript to native machine code, providing near-native performance.
  • npm Ecosystem: Access to over 2 million open-source packages via npm (Node Package Manager).
  • Best Use Cases: REST APIs, real-time chat apps, streaming services, CLI tools, microservices.
2 Installing Node.js & Running Your First Script
Terminal — Setup
# Download from https://nodejs.org (LTS recommended)
# Verify installation
node --version     # e.g. v20.11.0
npm --version      # e.g. 10.2.4

# Create and run a script
echo "console.log('Hello, Node.js!');" > hello.js
node hello.js
# Output: Hello, Node.js!
3 The global Object & process

Unlike browsers where the global object is window, Node.js exposes global and the process object for runtime information:

JavaScript — process object
console.log(process.version);       // Node.js version
console.log(process.platform);      // 'linux', 'win32', 'darwin'
console.log(process.argv);          // command-line arguments array
console.log(process.env.NODE_ENV);  // environment variable

// Exit the process with a code
process.exit(0); // 0 = success, non-zero = error
4 Code Challenge
Challenge: Write a Node.js script that reads process.argv to accept a name argument from the command line and prints "Hello, [name]!". Run it as node hello.js Balaji.