Skip to main content
Node.js Basics
CHAPTER 02 Beginner

Installing Node.js and Environment Setup

Updated: May 13, 2026
15 min read

# Installing Node.js and Setting Up Environment

Welcome to Chapter 2! You know the theory behind Node.js; now it's time to get your hands dirty. A backend developer's most important tool is their development environment. In this chapter, we'll walk you through installing Node.js, setting up a professional code editor, understanding NPM, and executing your very first Node.js code.

---

1. Introduction

To write and run Node.js code, you need two main pieces of software installed on your computer:

  1. 1. The Node.js Runtime: This is the engine that executes your JavaScript code on your machine.
  1. 2. A Code Editor: A specialized text editor designed for writing code. We highly recommend Visual Studio Code (VS Code).

When you install Node.js, it automatically installs npm (Node Package Manager), which is an essential tool you will use every day as a backend developer.

---

2. Learning Objectives

By the end of this chapter, you will be able to:

  • Download and install Node.js on your operating system (Windows, Mac, or Linux).
  • Verify the installation of Node.js and npm using the terminal.
  • Install and configure Visual Studio Code for Node.js development.
  • Understand and use the Node.js REPL (Read-Eval-Print Loop).
  • Write a JavaScript file and execute it using the Node command line tool.

---

3. Beginner-Friendly Explanations

Installing Node.js

Go to the official website: nodejs.org. You will usually see two versions available for download:
  1. 1. LTS (Long Term Support): This is the stable version. It's heavily tested and recommended for most users and production applications. Always choose LTS as a beginner.
  1. 2. Current: This has the latest features but might contain experimental bugs.

Download the LTS installer for your operating system and run it. You can just click "Next" through the default settings.

What is NPM?

When Node.js is installed, it sneaks another powerful tool onto your computer: npm. npm stands for Node Package Manager. Think of it as an app store for developers. If you need a specific piece of functionality (like connecting to a database or formatting a date), someone has probably already written code for it, and you can download it using npm.

What is a Terminal / CLI?

A CLI (Command Line Interface) or Terminal is a text-based interface to interact with your computer. Instead of clicking icons with a mouse, you type commands. Backend developers spend a lot of time in the terminal.
  • Windows: Command Prompt or PowerShell
  • Mac/Linux: Terminal

What is REPL?

REPL stands for Read-Eval-Print Loop. It is an interactive shell (a playground) that processes Node.js expressions immediately. You type code, press Enter, and it runs instantly.

---

4. Syntax Explanation

Let's look at terminal commands (Bash/PowerShell). These are not JavaScript, but commands you type into your terminal.

```bash id="ch2-bash-1" # Check which version of Node.js is installed node -v

# Check which version of npm is installed npm -v

1234567891011121314151617181920
**Output Explanation:**
Running `node -v` should print something like `v20.10.0`. If it says "command not found" or "node is not recognized", it means Node.js is not installed correctly or your computer needs to be restarted.

---

## 5. Real-world Examples

**Setting up a Professional Workspace**
Imagine a chef entering a messy kitchen versus a highly organized one. A professional developer ensures their workspace is perfectly tuned.
1. They use **VS Code** because it has built-in terminal support, meaning you don't have to switch back and forth between a separate terminal window and your code.
2. They use extensions in VS Code (like Prettier for code formatting) to write cleaner code automatically.

---

## 6. Multiple Code Examples

### Example 1: Using the REPL

Open your terminal and just type `node`, then press Enter.

bash id="ch2-bash-2" node

12
You will see a `>` prompt. You are now inside the Node.js environment. Try typing some JavaScript:

javascript id="ch2-repl-1" > 5 + 5 10 > console.log("Hello REPL!"); Hello REPL! undefined > [1, 2, 3].map(n => n * 2) [ 2, 4, 6 ]

12345678
To exit the REPL, type `.exit` and press Enter, or press `Ctrl + C` twice.

### Example 2: Running a Script File

Instead of using the REPL, we usually write our code in `.js` files. 

Create a file named `app.js` using VS Code.

javascript id="ch2-code-1" // app.js let age = 25; const name = "Alice";

console.log(Hello, my name is ${name} and I am ${age} years old.);

12
To run this file, open your terminal, navigate to the folder where `app.js` is saved, and type:

bash id="ch2-bash-3" node app.js

12
### Example 3: Doing basic math in a script

javascript id="ch2-code-2" // math.js const num1 = 150; const num2 = 300; const sum = num1 + num2;

console.log("The total sum is: " + sum);

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
Run it via terminal: `node math.js`

---

## 7. Output Explanations

When you execute `node app.js`, Node reads the file from top to bottom, evaluates the JavaScript, and prints any `console.log` statements directly into your terminal. 

In the REPL, you might notice the word `undefined` appearing after a `console.log()`. This is because the REPL always prints the *return value* of a function. Since `console.log()` doesn't return anything, it evaluates to `undefined`.

---

## 8. Common Mistakes

1. **Forgetting to save the file:** You write new code in VS Code, run `node app.js` in the terminal, and nothing changes. You must save the file (`Ctrl + S` or `Cmd + S`) before running the command!
2. **Wrong Directory:** Running `node app.js` when your terminal is in the `Documents` folder, but your file is in `Desktop`. You must use the `cd` (change directory) command to navigate to the correct folder first.
3. **Typing `node app.js` inside the REPL:** Once you type `node` and hit enter, you are writing JavaScript. If you type `node app.js` *inside* the REPL, you will get a syntax error.

---

## 9. Best Practices

- **Use VS Code's Integrated Terminal:** Go to `View > Terminal` or press `` Ctrl + ` `` in VS Code. This automatically opens the terminal in the correct folder!
- **Organize your files:** Create a dedicated folder like `C:\Development\NodeCourse` to keep all your code organized.
- **Auto-Save:** Consider turning on "Auto Save" in VS Code (`File > Auto Save`) to prevent the "forgot to save" mistake.

---

## 10. Exercises

1. Open your terminal and check your Node and npm versions.
2. Open the Node REPL, declare a variable `let x = 10;` and then multiply it by 5. Exit the REPL.
3. Create a new folder named `test-project`, open it in VS Code, and create a file named `test.js`. Write a `console.log` in it and run it using the integrated terminal.

---

## 11. Mini Project: Setup first Node.js project

**Objective:** Create a structured folder and your first runnable script.

**Steps:**
1. Open your terminal or command prompt.
2. Create a new directory: `mkdir node-basics`
3. Navigate into it: `cd node-basics`
4. Create a file (Mac/Linux): `touch index.js` (Windows PowerShell: `ni index.js`)
5. Open the folder in VS Code: `code .`
6. Inside `index.js`, write the following code:

javascript id="ch2-mini-project" // index.js console.log("--------------------------------"); console.log("Environment Setup Complete! ✅"); console.log("My Node.js version is:"); console.log(process.version); console.log("--------------------------------"); ``

  1. 7. Open the VS Code integrated terminal and run: node index.js.

---

12. Coding Challenges

Challenge 1: Create a file named loop.js. Write a for loop that prints numbers from 1 to 10. Run the file using the terminal.

Challenge 2: Create a file timer.js. Use the setTimeout function to print "Done!" after 3 seconds. Run it and watch the terminal wait for 3 seconds before finishing the program.

---

13. MCQs with Answers

Q1: Which command is used to check the installed version of Node.js? A) node --check B) node -v C) npm version D) node -version Answer: B

Q2: What is automatically installed alongside Node.js? A) React B) MongoDB C) npm D) VS Code Answer: C

Q3: What does REPL stand for? A) Read-Eval-Print Loop B) Run-Execute-Process Loop C) Read-Execute-Parse Loop D) Runtime-Environment-Process Loop Answer: A

Q4: How do you exit the Node.js REPL? A) Press Esc B) Type quit() C) Type .exit or press Ctrl + C twice D) Close the computer Answer: C

---

14. Interview Questions

  1. 1. What is NPM and why is it important?
*Answer:* NPM stands for Node Package Manager. It is the default package manager for Node.js, allowing developers to share, distribute, and consume reusable code libraries from a massive global registry.
  1. 2. What is the REPL in Node.js?
*Answer:* REPL stands for Read-Eval-Print Loop. It is a simple, interactive computer programming environment that takes user inputs, executes them, and returns the result to the user immediately.
  1. 3. What is the difference between installing LTS vs. Current versions of Node.js?
*Answer:* LTS (Long Term Support) is stable, heavily tested, and recommended for production. Current contains the latest features but may have bugs or experimental features that could break.

---

15. FAQs

Q: Do I need a powerful computer to run Node.js? A: Not at all! Node.js is very lightweight and runs perfectly fine on older laptops and lower-end machines.

Q: Why does my terminal say 'node is not recognized as an internal or external command'? A: This means your operating system cannot find the Node.js program. Try restarting your terminal or VS Code. If that fails, restart your computer. If it still fails, reinstall Node.js and ensure the "Add to PATH" option is checked during installation.

Q: Can I use Notepad to write Node.js code? A: Technically, yes. But it will be a miserable experience. VS Code provides syntax highlighting, auto-completion, and integrated terminals which are essential for developers.

---

16. Summary

  • You installed the Node.js runtime and npm.
  • The LTS version is always recommended for stability.
  • VS Code is the industry standard editor for JavaScript/Node.js developers.
  • The REPL allows you to test quick JavaScript snippets directly in the terminal.
  • To execute a file, you navigate to its folder in the terminal and run node filename.js`.

---

17. Next Chapter Recommendation

Now that your environment is fully set up and you know how to run files, let's start writing real backend code! In Chapter 3: Node.js Basics and First Program, we will refresh some crucial JavaScript concepts, learn how to handle variables and output in a Node environment, and build a Command Line Interface (CLI) greeting app.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·