Skip to main content
Express.js Tutorial
CHAPTER 02 Beginner

Setting Up Node.js and Express.js Environment

Updated: May 14, 2026
15 min read

# CHAPTER 2

Setting Up Node.js and Express.js Environment

1. Introduction

Before writing a single line of Express code, you must configure your computer to understand JavaScript outside of a web browser. This requires installing the Node.js runtime and its accompanying package manager (NPM). In this chapter, we will install the necessary software, initialize a backend project, and understand the standard folder structure used by professional Express developers.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Install Node.js and verify the installation in the terminal.
  • Initialize a project using npm init.
  • Install the express package via NPM.
  • Understand the role of package.json and node_modules.

3. Beginner-Friendly Explanation

Imagine you want to play a new video game.
  1. 1. First, you need the console (PlayStation/Xbox). Node.js is the console. It is the machine that physically runs the code.
  1. 2. Next, you need a store to download the games. NPM (Node Package Manager) is the digital store.
  1. 3. Finally, you need the actual game. Express.js is the game you download from the store to play on the console.

In backend development, you install the Node.js console once. Then, for every new project, you use NPM to download the Express framework.

4. Step 1: Installing Node.js

  1. 1. Visit the official website: nodejs.org.
  1. 2. Download the LTS (Long Term Support) version. This is the most stable version and is recommended for all production applications.
  1. 3. Run the installer and click "Next" through the default options.

Verify the Installation: Open your Terminal (Mac/Linux) or Command Prompt/PowerShell (Windows) and type:

bash
1
node -v

*(This will print the installed version, e.g., v20.x.x).*

Node.js automatically installs NPM. Verify it by typing:

bash
1
npm -v

5. Step 2: Initializing the Project

Create a new folder for your application and open it in your terminal.
bash
12
mkdir express-app
cd express-app

Now, initialize the project to create a package.json file:

bash
1
npm init -y

The package.json file is the ID card of your project. It records the project name and keeps a list of every third-party package you install.

6. Step 3: Installing Express.js

With the project initialized, we can download the Express framework from the NPM registry.
bash
1
npm install express

What just happened?

  1. 1. NPM connected to the internet and downloaded the Express codebase.
  1. 2. It created a folder called node_modules and placed the code inside.
  1. 3. It updated your package.json file to record that "Express" is now a dependency.

7. Step 4: Installing Nodemon (Development Tool)

Normally, if you change your code, you have to manually stop and restart the Node server. This is annoying. Nodemon is a tool that watches your files and restarts the server automatically when you click "Save".
bash
1
npm install nodemon --save-dev

*(The --save-dev flag means we only use this tool for local development, not on the live server).*

8. Step 5: Updating Scripts

Open your package.json file and modify the "scripts" section:
json
1234
"scripts": {
  "start": "node index.js",
  "dev": "nodemon index.js"
}

Now, to start your server, you simply type npm run dev in the terminal!

9. Standard Project Structure

Express doesn't force a structure, but professionals use the MVC (Model-View-Controller) pattern. Create these empty folders in your project:
  • models/: Database schemas (e.g., User, Post).
  • controllers/: Business logic.
  • routes/: URL definitions.
  • middleware/: Custom security or validation functions.
  • index.js or app.js: The main entry point of the server.

10. Best Practices

  • The .gitignore File: If you use Git/GitHub, you MUST create a .gitignore file and type node_modules/ inside it. The node_modules folder contains thousands of files and is massive. You never upload it. Because your package.json remembers what you installed, another developer simply types npm install and Node rebuilds the folder for them automatically.

11. Common Mistakes

  • Forgetting npm init: If you try to run npm install express without first running npm init, NPM won't know where to save the dependency record. Always initialize the project first.

12. Exercises

  1. 1. Explain the difference between npm install express and npm install nodemon --save-dev.

13. Coding Challenges

  • Challenge: Open your terminal, create a folder named api-challenge, initialize it with npm init -y, install express, and write a dev script in the package.json that uses nodemon.

14. MCQs with Answers

Question 1

What is the purpose of the package.json file?

Question 2

Which tool automatically restarts your Node.js server whenever you save changes to your code?

15. Interview Questions

  • Q: Explain the purpose of the node_modules directory. Why is it standard practice to exclude it from version control (Git)?
  • Q: What is the difference between a standard dependency and a devDependency in Node.js? Give an example of a package that belongs in each.

16. FAQs

Q: I see a file called package-lock.json. Should I delete it? A: NEVER delete package-lock.json. While package.json says "I need version 4 of Express", the lock file records the *exact* micro-version installed (e.g., 4.18.2). This ensures that if another developer runs the project on their computer, they get the exact same code, preventing version-mismatch bugs.

17. Summary

In Chapter 2, we established our backend laboratory. We installed the Node.js runtime and utilized the NPM registry to download Express.js. We learned how to initialize a project (npm init), manage dependencies via package.json, and set up developer tools like Nodemon to drastically improve our workflow. Finally, we outlined the standard MVC directory structure used by professionals.

18. Next Chapter Recommendation

Before writing code, we must understand how data flows through an Express application. Proceed to Chapter 3: Understanding Express.js Architecture.

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: ·