Skip to main content

Command Palette

Search for a command to run...

Getting Started with Node.js: A Beginner’s Guide

Updated
4 min readView as Markdown
Getting Started with Node.js: A Beginner’s Guide

Node.js has revolutionized the way we think about JavaScript, taking it out of the browser and onto the server. If you've been wanting to build backend applications using the language you already know and love, you're in the right place.

Here is a step-by-step guide to setting up your very first Node.js environment and running your first server.

Installing Node.js

Before you can run JavaScript on your machine, you need the Node.js runtime.

  • How to install: Visit the official Node.js website.

  • Recommendation: Download the LTS (Long Term Support) version. It is the most stable version and is recommended for most users.

  • Platform Neutrality: Whether you are on Windows, macOS or Linux, the installer will handle the environment variables for you, making node accessible from your command line.

Verifying the Installation

Once the installation is complete, you need to ensure your system recognizes Node.js. Open your terminal (Command Prompt, PowerShell, or Terminal) and type the following commands:

  • To check the Node version: node -v

  • To check the npm (Node Package Manager) version: npm -v

If you see version numbers (e.g., v20.x.x), you are ready to go!

Understanding the Node REPL

Before we write a file, let's look at the REPL.

REPL stands for Read-Eval-Print-Loop. It is an interactive shell that takes your JavaScript code, evaluates it, and prints the result immediately. It's a fantastic place to test small logic snippets without creating a file.

How to use it:

  1. Type node in your terminal and hit Enter.

  2. You'll see a prompt (usually >).

  3. Type 2+2 and hit Enter. It will output 4.

  4. Type .exit or press Ctrl + C twice to leave.

Creating and Running Your First JS File

While the REPL is fun, real applications live in files.

  1. Create a file: Open your favorite text editor (like VS Code) and create a new file named app.js.

  2. Add logic: Write a simple line of code:

console.log("Node.js is running successfully!");
  1. Run the script: Go back to your terminal, navigate to the folder where you saved the file, and run:
node app.js

You should see your message printed directly in the terminal.

Writing a "Hello World" Server

To truly understand Node's power, let's build a basic web server. We will use the built-in http module, so no external frameworks (like Express) are needed yet.

Create a file named server.js and follow these steps:

  1. Import the HTTP module: This allows Node to transfer data over the HyperText Transfer Protocol.

  2. Create the server: Use the createServer method which takes a function with a request (req) and a response (res).

  3. Set the headers and content: Tell the browser the request was successful (Status 200) and send "Hello World".

  4. Listen on a port: Tell the server to stay active on a specific port (e.g., 3000).

Conceptual Implementation

const http = require('http');

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World from my first Node server!');
});

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

Execution

Run node server.js in your terminal. You will see the console log. Now, open your web browser and go to http://localhost:3000. You’ve just successfully launched your first backend server!

Key Takeaways

  • Node.js allows JavaScript to run on your local machine/server.

  • The REPL is perfect for quick experimentation.

  • The node command is your primary tool for executing .js files.

  • The http module is the foundation of web networking in Node.js.

By mastering these basics, you've built the foundation necessary to dive into more complex topics like file systems, asynchronous programming, and eventually, full-scale APIs.

1 views