Installing Node.js and Environment Setup
# 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. The Node.js Runtime: This is the engine that executes your JavaScript code on your machine.
- 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. 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.
- 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
bash id="ch2-bash-2" node
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 ]
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.);
bash id="ch2-bash-3" node app.js
javascript id="ch2-code-2" // math.js const num1 = 150; const num2 = 300; const sum = num1 + num2;
console.log("The total sum is: " + sum);
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("--------------------------------");
``
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. What is NPM and why is it important?
- 2. What is the REPL in Node.js?
- 3. What is the difference between installing LTS vs. Current versions of Node.js?
---
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.