Getting Started with Node.js
Node.js lets you run JavaScript outside the browser. Instead of relying on a browser like Chrome or Firefox, you can execute JavaScript on your computer, build servers, and create command-line tools. This opens up an entire world of possibilities for developers who already know JavaScript.
What Is Node.js?
Node.js is a JavaScript runtime built on Chrome’s V8 engine. When Google improved Chrome’s performance with V8, Ryan Dahl saw an opportunity to run JavaScript on the server. He created Node.js in 2009, and it changed how developers build web applications.
Before Node.js, you needed different languages for front-end and back-end development. If you knew JavaScript, you could only work in the browser. Now you can use JavaScript for everything—servers, databases, build tools, and desktop applications.
Node.js excels at handling concurrent connections. Its event-driven, non-blocking I/O model makes it efficient for real-time applications, APIs, and streaming services. Companies like Netflix, LinkedIn, and Walmart use Node.js in production.
Installing Node.js
The easiest way to install Node.js is through nvm (Node Version Manager). nvm lets you switch between different Node versions easily, which helps when working on projects that require specific versions.
# Install nvm (macOS/Linux)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
# Restart your terminal, then install Node.js
nvm install 20
nvm use 20
On Windows, download the installer from nodejs.org or use a tool like nvm-windows. After installation, verify everything works:
node --version
npm --version
These commands should display version numbers. node runs your JavaScript code, and npm (Node Package Manager) lets you install third-party libraries.
Your First Node.js Script
Create a file called hello.js and add this code:
console.log("Hello from Node.js!");
const now = new Date();
console.log(`Current time: ${now.toISOString()}`);
Run it from your terminal:
node hello.js
You should see the message printed to your terminal. Congratulations—you just ran JavaScript outside a browser.
Understanding the REPL
Node.js includes a REPL (Read-Eval-Print Loop) for experimenting with code. Type node in your terminal without any file to enter it:
node
The prompt changes to >. You can type JavaScript directly:
> const x = 10
> const y = 20
> x + y
30
> console.log("Hello!")
Hello!
Type .exit or press Ctrl+C twice to leave the REPL. The REPL is useful for quick tests and exploring JavaScript APIs.
Using npm Basics
npm comes bundled with Node.js. It gives you access to thousands of open-source packages—libraries that other developers have created. Let’s create a simple project and install a package:
# Create a new project
mkdir my-project
cd my-project
npm init -y
This creates a package.json file, which tracks your project dependencies. Now install a popular package called chalk (for colored terminal output):
npm install chalk
Create a script to use it:
import chalk from 'chalk';
console.log(chalk.green('Success!'));
console.log(chalk.red.bold('Error:'), 'Something went wrong');
Run it:
node index.js
You’ll see colored text in your terminal. This demonstrates how easy it is to bring external code into your projects.
A Practical Example: Simple HTTP Server
Node.js can create web servers without external libraries. Here’s a basic HTTP server:
import http from '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/');
});
Run this script and open your browser to http://localhost:3000. You’ve just built a working web server in about six lines of code.
This server responds to every request with the same message. In later tutorials, you’ll learn how to handle different routes, serve HTML pages, and build APIs with Express.
Summary
Node.js extends JavaScript beyond the browser, letting you build servers, CLI tools, and desktop applications. You now know how to:
- Install Node.js using nvm
- Run JavaScript files with the
nodecommand - Use the REPL for interactive testing
- Install packages with npm
- Create a basic HTTP server
In the next tutorial, we’ll explore Node.js modules and the CommonJS system for organizing code.