Node.js Setting Up the Environment

Before writing any Node.js code, the runtime environment must be installed on the computer. This topic walks through everything needed to get Node.js up and running — from downloading the installer to writing and executing the first program.

What Is Needed to Get Started?

To begin working with Node.js, the following are required:

  • A computer running Windows, macOS, or Linux.
  • An internet connection (for downloading Node.js and packages).
  • A text editor or code editor (such as Visual Studio Code).
  • Access to a terminal or command prompt.

Step 1 – Download and Install Node.js

Visiting the Official Website

Go to the official Node.js website: https://nodejs.org

Two versions are typically available for download:

  • LTS (Long-Term Support): This is the recommended version for most users. It is stable, well-tested, and supported for a longer period. Beginners and production environments should use this version.
  • Current: This version includes the latest features but may not be as stable. It is suitable for developers who want to experiment with new capabilities.

Installing on Windows

  1. Download the Windows Installer (.msi file) from the Node.js website.
  2. Run the installer and follow the on-screen instructions.
  3. Make sure the option to install npm (Node Package Manager) is checked — it is included by default.
  4. Click "Finish" once the installation is complete.

Installing on macOS

  1. Download the macOS Installer (.pkg file) from the Node.js website.
  2. Open the downloaded file and follow the installation steps.
  3. Alternatively, use a package manager like Homebrew by running: brew install node

Installing on Linux (Ubuntu/Debian)

Open the terminal and run the following commands:

sudo apt update
sudo apt install nodejs npm

For the latest version, the NodeSource repository can be used instead:

curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt install -y nodejs

Step 2 – Verify the Installation

After installation, open the terminal (Command Prompt on Windows, Terminal on macOS/Linux) and run the following commands to confirm that Node.js and npm are correctly installed:

node --version

Expected output (version number may vary):

v20.11.0
npm --version

Expected output:

10.2.4

If both commands return version numbers without errors, the installation was successful.

Step 3 – Set Up a Code Editor

Recommended Editor: Visual Studio Code (VS Code)

Visual Studio Code is the most popular editor among Node.js developers. It is free, lightweight, and has excellent support for JavaScript and Node.js through extensions.

Download VS Code from: https://code.visualstudio.com

Useful VS Code Extensions for Node.js

  • ESLint: Highlights code errors and enforces coding standards.
  • Prettier: Automatically formats code for consistency.
  • Node.js Extension Pack: A bundle of helpful tools for Node.js development.
  • Path Intellisense: Auto-completes file paths in code.

Step 4 – Create and Run the First Node.js File

Creating the Project Folder

Create a new folder anywhere on the computer to hold the project. For example, create a folder named my-first-node-app.

Open the folder in VS Code using:

code my-first-node-app

Or open VS Code and use File → Open Folder to navigate to the folder.

Creating the First JavaScript File

Inside the folder, create a new file named app.js. Add the following code:

// app.js
console.log("Node.js is set up and running!");
console.log("Node version: " + process.version);

Running the File from the Terminal

Open the terminal inside VS Code (use Terminal → New Terminal) and type:

node app.js

Expected output:

Node.js is set up and running!
Node version: v20.11.0

Understanding the Project Structure

As projects grow, files are organized into folders. A simple Node.js project might look like this:

my-first-node-app/
│
├── app.js          ← Main entry point of the application
├── package.json    ← Project metadata and dependencies (created with npm init)
└── node_modules/   ← Installed packages (created automatically by npm)

The package.json file is the heart of any Node.js project. It stores information about the project like its name, version, and which packages it depends on. This file is covered in detail in the npm topic.

Using NVM (Node Version Manager) – Optional but Recommended

When working on multiple projects, different Node.js versions might be required. NVM (Node Version Manager) allows easy switching between versions.

Installing NVM on macOS/Linux

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

Installing a Specific Node.js Version with NVM

nvm install 20
nvm use 20
nvm alias default 20

NVM is not mandatory for beginners, but it is a good habit to learn early for managing projects professionally.

Key Points

  • Node.js is installed from the official website at https://nodejs.org. The LTS version is recommended for most users.
  • After installation, use node --version and npm --version in the terminal to confirm success.
  • Visual Studio Code is the most widely used editor for Node.js development.
  • A Node.js program is run from the terminal using the node filename.js command.
  • The package.json file is a key part of every Node.js project and is covered in the npm topic.
  • NVM is an optional but useful tool for managing multiple Node.js versions on the same machine.

Leave a Comment

Your email address will not be published. Required fields are marked *