Node.js Setup

Setting up Node.js on your system is the first step to leveraging its power for building scalable, efficient applications. Unlike traditional web environments where JavaScript runs only in the browser, Node.js allows you to run JavaScript on servers, enabling you to build everything from CLI tools to massive real-time chat applications. Follow the guide below to install Node.js and get started with your first application.

Developer Tip: Node.js uses an asynchronous, event-driven model. This makes it incredibly fast for I/O intensive tasks like handling thousands of concurrent database queries or streaming video.

Prerequisites

  • A computer running Windows, macOS, or a Linux distribution (like Ubuntu).
  • Basic knowledge of JavaScript (variables, functions, and objects).
  • A code editor installed, such as Visual Studio Code, which has excellent built-in support for Node.js debugging.

 

Steps to Set Up Node.js

1. Download Node.js

  • Visit the Node.js official website.
  • You will see two versions: LTS (Long-Term Support) and Current. For most developers and production environments, we recommend the LTS version because it is more stable and receives security updates for a longer period.
  • Download the installer specifically for your operating system.
Best Practice: Use the LTS version for your professional projects. Only use the "Current" version if you need to test the latest cutting-edge features that aren't yet available in the stable release.

2. Install Node.js

  • Run the downloaded installer (.msi for Windows or .pkg for macOS).
  • Follow the on-screen instructions. Usually, the default settings are perfect for most developers. Ensure the "Add to PATH" checkbox is checked, as this allows you to run Node commands from any terminal window.
  • Verify the installation by opening your terminal (Command Prompt, PowerShell, or Terminal on Mac) and running:
node -v
npm -v

Output:

v20.10.0
10.2.3
Watch Out: If your terminal says "command not found," you may need to restart your terminal or manually add the Node.js installation path to your system's Environment Variables.

3. Set Up a Node.js Project

  • Every Node.js project needs its own folder to keep its files and dependencies organized. Create a new directory and move into it:
mkdir my-node-app
cd my-node-app
  • Initialize a new Node.js project using NPM (Node Package Manager). NPM is installed automatically with Node and helps you manage external libraries:
npm init -y

Output: 

A package.json file will be created in your folder. This file is the "manifest" of your project—it lists your project name, version, and the packages your app needs to run.

Common Mistake: Beginners often forget to run npm init. Without this, you won't be able to easily install libraries (like Express or Mongoose) or share your project with other developers.

4. Write Your First Node.js Application

  • Create a file named app.js in your project folder using your editor or the terminal:
touch app.js
  • Open app.js in your code editor and add the following code to create a basic web server:
const http = require('http');

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

server.listen(3000, '127.0.0.1', () => {
  console.log('Server running at http://localhost:3000/');
});
Developer Tip: While Node.js has a built-in http module as shown above, most real-world developers use a framework called Express to make building servers much faster and cleaner.

5. Run the Application

  • Return to your terminal and start your server by running:
node app.js
  • Open your browser and navigate to http://localhost:3000. You should see the message "Hello, Node.js!" printed on the screen.
  • To stop the server, go back to your terminal and press Ctrl + C.
Watch Out: If you get an "EADDRINUSE" error, it means another program is already using port 3000. You can change the port number in your code to 3001 or 4000 to fix this.

 

Summary

Node.js setup involves downloading and installing the runtime, initializing a project with NPM, and creating your first application script. Once installed, you have access to a massive ecosystem of packages through the NPM registry. Whether you're building lightweight APIs, robust web servers, or real-time streaming apps, Node.js provides the high-performance foundation required for modern web development. Now that you're set up, your next step is to explore NPM packages to add more functionality to your app!