Master Node.js From Scratch

Clear, interactive, and structured Node.js lessons designed for beginners and developers.

COURSE SYLLABUS
1. Introduction to Node.js
Introduction to Node.js What Is Node.js? Node.js Architecture Features of Node.js Node.js vs Browser JavaScript Node.js Use Cases
2. Installation and Setup
Installation Introduction LTS and Current Versions npm Setup Environment Setup Verify Installation Node.js REPL
3. Node.js Fundamentals
Fundamentals Introduction Running JavaScript Global Objects Process Object Command-Line Arguments Environment Variables
4. JavaScript Basics
JavaScript Basics Introduction Variables: var, let and const JavaScript Data Types Comparison and Logical Operators If, Else If, Else and Switch Loops and Iteration Functions and Arrow Functions JavaScript Array Methods
5. Node.js Modules
Modules Introduction CommonJS Modules ES Modules Export and Import Built-in Modules Creating Custom Modules Path Module OS Module
6. Working with JSON
JSON Introduction JSON Syntax JSON.stringify() JSON.parse() JSON Array of Objects JSON User Database
7. Node Package Manager
npm Introduction package.json Installing Packages Dependencies and DevDependencies npm Scripts Package Versions and Semantic Versioning npm init and Project Setup
8. File System
File System Introduction Reading Files Writing Files Appending Files Deleting Files Working with Directories
9. CLI Applications
CLI Applications Introduction Command-Line Input Task Manager Project Structure Reading todos.json Adding, Updating and Deleting Tasks Saving Tasks to JSON
10. Events and EventEmitter
Events Introduction EventEmitter Registering Events Emitting Events Removing Event Listeners Custom Events
11. Asynchronous Programming
Async Programming Introduction Callbacks Promises async and await setTimeout() and setInterval() Event Loop and Non-Blocking I/O
12. Native HTTP Module
HTTP Module Introduction Creating an HTTP Server Handling Requests Sending Responses HTTP Request Methods Status Codes and Headers Handling POST Request Body Parsing Incoming JSON Products JSON API
13. Express.js
Express.js Introduction Installing Express Creating an Express Server Request and Response Objects Route Parameters and Query Strings Serving Static Files
14. Express Middleware
Middleware Introduction Built-in Middleware Application-Level Middleware Router-Level Middleware Custom Middleware Error-Handling Middleware
15. REST API Development
REST API Introduction GET Endpoints POST Endpoints PUT and PATCH Endpoints DELETE Endpoints Request Validation and API Responses
16. Database Integration
Database Introduction MySQL Integration MongoDB Integration Database CRUD Operations Database Queries and Filtering Connection Management
17. Authentication and Authorization
Authentication Introduction Password Hashing User Registration User Login JWT Authentication Authorization and Protected Routes
18. Advanced Node.js
Advanced Node.js Introduction Error Handling Debugging Node.js Applications Streams Buffers Performance and Security

Dependencies and DevDependencies in Node.js

In Node.js, dependencies are external packages used by an application to perform its functionality. NPM helps developers install and manage these packages through the package.json file.

NPM provides two important categories of packages: dependencies and devDependencies. Understanding the difference helps developers organize projects, install the required packages, and prepare applications for deployment.

Learning objective: Understand dependencies and development dependencies, learn how to install each type, examine package.json, and use these packages in a practical Node.js project.

1. What Is a Dependency?

A dependency is a package that an application relies on to perform a particular task. Instead of writing every feature from scratch, developers can install existing packages and use their functionality.

For example:

  • Express helps build web servers and REST APIs.
  • Mongoose helps applications work with MongoDB.
  • Axios provides an API for making HTTP requests.

When an application requires a package while running, that package is normally recorded under dependencies.

Node.js Application

Application features and business logic

↓
dependencies

Packages required by the application

↓
Runtime Functionality

The application uses installed packages

2. What Is a DevDependency?

A development dependency is a package mainly used while developing, testing, formatting, or building an application. These tools are generally not needed by the application to perform its normal production functionality.

Common examples include:

  • Nodemon — restarts a Node.js application when watched files change.
  • ESLint — checks JavaScript code for problems and style issues.
  • Prettier — formats source code.
  • Jest — provides tools for automated testing.

These packages are normally recorded under devDependencies in package.json.

Developer

Writes and tests application code

↓
devDependencies

Testing, linting, formatting, and development tools

↓
Development Workflow

Helps improve code quality and productivity

3. Difference Between Dependencies and DevDependencies

Dependencies DevDependencies
Packages required by the application during normal runtime. Packages mainly used during development and testing.
Declared under dependencies. Declared under devDependencies.
Example: Express in a web server. Example: Nodemon for development.
Installed using npm install package-name. Installed using npm install package-name --save-dev.
Usually required in a production installation. Can be omitted when installing only production dependencies.
Important: The package category depends on how the project uses it. A package that is normally a development tool could still be needed at runtime in a particular application. The classification should reflect the actual project requirements.

4. Install a Dependency

To install a package as a normal dependency, use the npm install command.

Syntax

Terminal
npm install package-name

Example: Install Express

Terminal
npm install express

This command normally:

  • Downloads Express and its required dependencies.
  • Creates or updates the node_modules folder.
  • Adds Express to dependencies in package.json.
  • Updates package-lock.json.

Example package.json

JSON
{
  "name": "studentapp",
  "version": "1.0.0",
  "dependencies": {
    "express": "^4.21.0"
  }
}

The Express version shown here is illustrative. NPM records the version range corresponding to the package version installed.

5. Install a DevDependency

To install a package mainly used for development, use the --save-dev option.

Syntax

Terminal
npm install package-name --save-dev

Example: Install Nodemon

Terminal
npm install nodemon --save-dev

Nodemon monitors specified files and restarts a Node.js process when relevant changes are detected. It is commonly used during development.

Example package.json

JSON
{
  "name": "studentapp",
  "version": "1.0.0",
  "devDependencies": {
    "nodemon": "^3.0.0"
  }
}

The version above is an example. Your installed version may differ.

6. Understand Both Properties in One Project

A project can have both dependencies and devDependencies. Each category is stored separately in package.json.

Install both packages

Terminal
npm install express
npm install nodemon --save-dev

Resulting package.json structure

JSON
{
  "name": "studentapp",
  "version": "1.0.0",
  "dependencies": {
    "express": "^4.21.0"
  },
  "devDependencies": {
    "nodemon": "^3.0.0"
  }
}

Your actual package.json may contain additional fields such as scripts and the exact package versions installed by NPM.

7. Practical Example: Express with Nodemon

Let's create a small application that uses Express as a dependency and Nodemon as a development tool.

Step 1: Create the project

Terminal
mkdir StudentApp
cd StudentApp
npm init -y

Step 2: Install the packages

Terminal
npm install express
npm install nodemon --save-dev

Step 3: Create app.js

JavaScript
const express = require("express");

const app = express();

app.get("/", (req, res) => {
    res.send("Welcome to StudentApp!");
});

app.get("/students", (req, res) => {
    res.json([
        { id: 1, name: "Samadhan" },
        { id: 2, name: "Pradnya" }
    ]);
});

app.listen(3000, () => {
    console.log("StudentApp running on port 3000");
});

Step 4: Add scripts in package.json

Keep the dependencies installed by NPM and add the following entries inside the existing scripts property:

JSON
"scripts": {
  "start": "node app.js",
  "dev": "nodemon app.js"
}

If a scripts property already exists, replace or update its contents rather than adding a second scripts property.

Step 5: Run in development mode

Terminal
npm run dev

Expected terminal message:

Output
StudentApp running on port 3000

Nodemon keeps the application running and restarts it when watched files change. The terminal may also show additional Nodemon messages.

Step 6: Run normally

Terminal
npm start

This executes node app.js directly without Nodemon.

Expected API responses

GET request to the root route:

Response
Welcome to StudentApp!

GET request to the students route:

JSON Response
[
  {
    "id": 1,
    "name": "Samadhan"
  },
  {
    "id": 2,
    "name": "Pradnya"
  }
]

8. Install Only Production Dependencies

When deploying an application, development tools may not be required. NPM supports installing only production dependencies.

Terminal
npm install --omit=dev

This command omits packages declared in devDependencies from the installation. Packages declared in dependencies remain included.

Important: Do not omit development dependencies when your deployment or build process requires them. Some projects need development tools to compile, bundle, or generate production assets.

9. How NPM Manages Package Installation

Install a Package

npm install express

↓
Update package.json

Declare the dependency category

↓
Update package-lock.json

Record resolved package versions

↓
Install Package Files

Store installed packages in node_modules

10. Common Mistakes

Mistake Why It Is a Problem Correct Approach
Installing Nodemon as a production dependency without a reason It is usually a development tool. Use --save-dev when appropriate.
Forgetting to install Express The application cannot resolve the required package. Run npm install express.
Writing two scripts properties Duplicate JSON property names can cause confusing configuration. Keep one scripts object and add all commands inside it.
Deleting package-lock.json without a reason Dependency resolution may change when reinstalling. Keep the lockfile under version control for applications.
Assuming devDependencies are never used in production workflows Build tools may be required during deployment. Understand the project's build and deployment process.

11. Practice Tasks

  1. Create a project named EmployeeApp.
  2. Install Express as a dependency.
  3. Install Nodemon as a devDependency.
  4. Check package.json and identify both categories.
  5. Create a simple Express route that returns employee data.
  6. Add start and dev scripts.
  7. Run the application using npm start.
  8. Run the application using npm run dev.

Summary

Dependencies are packages normally required by an application at runtime, while devDependencies are mainly used for development, testing, formatting, and build tasks. The npm install command installs regular dependencies, and npm install package-name --save-dev installs development dependencies. NPM records these categories in package.json and tracks resolved versions in package-lock.json. Understanding both categories helps developers manage packages, run applications, and prepare Node.js projects for deployment at CIIT TRAINING INSTITUTE.