Introduction to Node.js
1.1 What is Node.js?
Node.js is an open-source, cross-platform JavaScript runtime environment that allows developers to execute JavaScript code outside the browser. Built on Chrome’s V8 JavaScript engine, Node.js enables the development of scalable and high-performance applications, especially for backend services like APIs, microservices, and real-time applications.
1.2 History and Evolution of Node.js
Node.js was created by Ryan Dahl in 2009 as a solution to the limitations of synchronous programming in traditional web servers. Before Node.js, most web servers were blocking (synchronous), leading to performance bottlenecks. Ryan Dahl introduced the event-driven, non-blocking I/O model to improve performance and scalability. Over time, Node.js gained popularity and became a critical tool in modern web development.
Key Milestones in Node.js History
- 2009 – Node.js was released by Ryan Dahl.
- 2010 – npm (Node Package Manager) was introduced.
- 2015 – Node.js Foundation was formed; io.js was merged back into Node.js.
- 2018 – Node.js introduced worker threads for multi-threading support.
- 2021 - Present – Node.js continues to evolve with new ECMAScript features and performance improvements.
1.3 Why Use Node.js?
Node.js offers several advantages that make it a preferred choice for backend development:
Performance and Scalability
- Non-blocking, Asynchronous I/O – Handles multiple requests simultaneously without waiting for previous operations to complete.
- Event-driven Architecture – Efficiently manages concurrent connections, making it ideal for real-time applications.
- Built on the V8 Engine – Executes JavaScript at high speed, as V8 compiles JavaScript into machine code.
Unified JavaScript Stack
- With Node.js, developers can use JavaScript for both frontend and backend development, leading to a seamless development experience.
Large Ecosystem and Community Support
- npm (Node Package Manager) provides thousands of reusable modules.
- Active community with extensive resources and frequent updates.
Use Cases of Node.js
- Backend for Web Applications – RESTful APIs, GraphQL APIs, authentication systems.
- Real-Time Applications – Chat applications, collaboration tools, gaming servers.
- Microservices – Lightweight and modular architecture for scalable applications.
- Command-Line Tools – Build automation scripts and CLI utilities.
1.4 Installation and REPL
Installing Node.js
To start building with Node.js, the first step is installing it on your machine.
Installation Methods
1. Official Installer (Recommended for Beginners)
- Visit https://nodejs.org
- Download the LTS version (recommended for most users)
- Follow the installation steps according to your OS (Windows, macOS, or Linux)
2. Using Node Version Manager (NVM)
Great for developers who need to switch between Node versions.
macOS/Linux:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc # or ~/.zshrc depending on your shell
nvm install --lts
nvm use --lts
Windows: Use nvm-windows Windows: Use nvm-windows
Verifying Installation
After installation, confirm it's working:
node -v # Displays Node.js version
npm -v # Displays npm version
Understanding the REPL
REPL stands for Read-Eval-Print Loop, and it's an interactive shell for running Node.js code snippets.
How to Start the REPL
Open your terminal and type:
node
You will enter the interactive environment where you can execute JavaScript/Node.js code line by line.
REPL Workflow
- Read: Takes your input.
- Eval: Evaluates the input.
- Print: Outputs the result.
- Loop: Waits for the next input.
Sample REPL Session
> 2 + 3
5
> const name = "Odinaka"
undefined
> name.toUpperCase()
'ODINAKA'
Useful REPL Commands
- .help – Displays REPL commands.
- .exit – Exits the REPL.
- _ – Refers to the last evaluated result.
REPL is a great tool for experimenting with Node.js features and testing logic without creating files. It's especially handy for quick debugging and learning the language interactively.
1.5 Writing and Running Your First Node.js Script
Conclusion
Node.js has revolutionized backend development by providing a fast, scalable, and efficient environment for running JavaScript outside the browser. Its event-driven, non-blocking architecture makes it an excellent choice for modern applications. With a strong ecosystem and a unified JavaScript stack, Node.js continues to be a dominant force in web development.