Setting Up Node.js and Express.js Environment
# 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
expresspackage via NPM.
-
Understand the role of
package.jsonandnode_modules.
3. Beginner-Friendly Explanation
Imagine you want to play a new video game.- 1. First, you need the console (PlayStation/Xbox). Node.js is the console. It is the machine that physically runs the code.
- 2. Next, you need a store to download the games. NPM (Node Package Manager) is the digital store.
- 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. Visit the official website: nodejs.org.
- 2. Download the LTS (Long Term Support) version. This is the most stable version and is recommended for all production applications.
- 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:
*(This will print the installed version, e.g., v20.x.x).*
Node.js automatically installs NPM. Verify it by typing:
5. Step 2: Initializing the Project
Create a new folder for your application and open it in your terminal.Now, initialize the project to create a package.json file:
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.What just happened?
- 1. NPM connected to the internet and downloaded the Express codebase.
-
2.
It created a folder called
node_modulesand placed the code inside.
-
3.
It updated your
package.jsonfile 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".*(The --save-dev flag means we only use this tool for local development, not on the live server).*
8. Step 5: Updating Scripts
Open yourpackage.json file and modify the "scripts" section:
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.jsorapp.js: The main entry point of the server.
10. Best Practices
-
The
.gitignoreFile: If you use Git/GitHub, you MUST create a.gitignorefile and typenode_modules/inside it. Thenode_modulesfolder contains thousands of files and is massive. You never upload it. Because yourpackage.jsonremembers what you installed, another developer simply typesnpm installand Node rebuilds the folder for them automatically.
11. Common Mistakes
-
Forgetting
npm init: If you try to runnpm install expresswithout first runningnpm init, NPM won't know where to save the dependency record. Always initialize the project first.
12. Exercises
-
1.
Explain the difference between
npm install expressandnpm install nodemon --save-dev.
13. Coding Challenges
-
Challenge: Open your terminal, create a folder named
api-challenge, initialize it withnpm init -y, installexpress, and write adevscript in thepackage.jsonthat usesnodemon.
14. MCQs with Answers
What is the purpose of the package.json file?
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_modulesdirectory. Why is it standard practice to exclude it from version control (Git)?
-
Q: What is the difference between a standard dependency and a
devDependencyin Node.js? Give an example of a package that belongs in each.
16. FAQs
Q: I see a file calledpackage-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.