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

Package Versioning and Semantic Versioning

Package versioning helps developers identify different releases of a software package. In Node.js projects, NPM uses package versions to install, update, and manage dependencies.

Semantic Versioning, also called SemVer, is a commonly used versioning convention that represents changes through three numbers: MAJOR, MINOR, and PATCH.

Learning objective: Understand package versions, semantic versioning, version ranges, the caret and tilde symbols, and commands used to inspect and update package versions.

1. What Is Package Versioning?

Package versioning is the practice of assigning a version number to each release of a package. When developers improve a package, fix a bug, or introduce a new feature, they may publish a new version.

For example, a package might have these releases:

Example Versions
1.0.0
1.0.1
1.1.0
2.0.0

These version numbers can communicate the type of change made between releases when the package follows Semantic Versioning.

Package Development

New features and bug fixes

↓
New Package Version

Example: 1.0.0 → 1.1.0

↓
NPM Dependency Management

Choose compatible package releases

2. What Is Semantic Versioning?

Semantic Versioning provides a standard way to describe package releases. A typical stable version has three numeric parts:

Version Format
MAJOR.MINOR.PATCH

Example: 2.4.3
Part Meaning Typical Reason to Increase
MAJOR Incompatible API changes. Breaking changes are introduced.
MINOR New functionality that remains backward-compatible. A compatible feature is added.
PATCH Backward-compatible bug fixes. A compatible bug fix is released.
Important: Semantic Versioning describes the intended compatibility of a release. The actual compatibility still depends on how the package developers follow the convention.

3. Understand MAJOR, MINOR, and PATCH

A. MAJOR Version

Increase the major version when a release introduces incompatible changes to the public API.

Example
1.5.2 → 2.0.0

For example, a library might remove a method that existing applications depend on. Applications using the old method may need code changes before upgrading.

B. MINOR Version

Increase the minor version when adding new backward-compatible functionality.

Example
1.5.2 → 1.6.0

For example, a library adds a new method while keeping its existing methods working as before.

C. PATCH Version

Increase the patch version when fixing a bug without introducing incompatible changes.

Example
1.5.2 → 1.5.3

For example, a calculation error in a library is fixed without changing the expected behavior of its public API.

4. How to Read a Version Number

Consider this version:

Version
3.7.4
  • 3 is the major version.
  • 7 is the minor version.
  • 4 is the patch version.

If the next release is a compatible bug fix, it might become 3.7.5. If a new backward-compatible feature is added, it might become 3.8.0. An incompatible API change might result in 4.0.0.

3
MAJOR

Breaking change

7
MINOR

New feature

4
PATCH

Bug fix

5. What Is a Version Range?

A version range describes which package versions are acceptable for a dependency. NPM uses version ranges in package.json to determine which releases can satisfy the dependency declaration.

For example:

JSON
{
  "dependencies": {
    "example-package": "^1.2.3"
  }
}

The caret symbol indicates a range that generally allows compatible updates while keeping the major version at 1. The precise set of versions accepted depends on Semantic Versioning rules and the declared version.

6. Understanding the Caret (^) Symbol

The caret symbol ^ allows compatible updates according to Semantic Versioning rules. For a version such as ^1.2.3, the allowed range is at least 1.2.3 but less than 2.0.0.

Version Range
^1.2.3

>= 1.2.3 and < 2.0.0

Examples of versions that satisfy this range include:

  • 1.2.3
  • 1.3.0
  • 1.9.9

A stable version such as 2.0.0 is outside this range because it changes the major version.

For versions beginning with zero, caret behavior is more restrictive. For example, ^0.2.3 allows versions from 0.2.3 up to, but not including, 0.3.0.

7. Understanding the Tilde (~) Symbol

The tilde symbol ~ generally permits patch updates while keeping the minor version fixed for a normal three-part stable version.

Version Range
~1.2.3

>= 1.2.3 and < 1.3.0

Examples of versions that satisfy this range include:

  • 1.2.3
  • 1.2.4
  • 1.2.9

Versions such as 1.3.0 and 2.0.0 are outside this range.

8. Difference Between ^ and ~

Symbol Example Range for a Stable Version
^ ^1.2.3 >=1.2.3 <2.0.0
~ ~1.2.3 >=1.2.3 <1.3.0
No range symbol 1.2.3 Exact version requirement

A caret generally permits more updates than a tilde. Both range operators help balance package updates with compatibility requirements.

9. Check Installed Package Versions

NPM provides commands to inspect installed packages and their versions.

Display all top-level packages

Terminal
npm list --depth=0

Example output:

Output
studentapp@1.0.0
+-- express@4.21.0
`-- nodemon@3.1.0

These are illustrative package versions. Your output depends on the packages and versions installed in your project.

Check one package

Terminal
npm list express

Check outdated packages

Terminal
npm outdated

The outdated command compares installed packages with versions available under the project's dependency requirements and registry information.

10. Update a Package

To update dependencies within their declared version ranges, use the NPM update command.

Terminal
npm update

NPM updates eligible dependencies according to the version requirements in package.json and updates the lockfile. It does not necessarily install the latest major release.

Install a specific version

Terminal
npm install express@4.21.0

This requests the specified Express version. Check compatibility and security requirements before selecting a version for a real application.

11. Understand package-lock.json

The package.json file declares the dependency requirements. The package-lock.json file records the resolved dependency tree, including exact package versions, to make installations more reproducible.

package.json

Declares dependency ranges

↓
package-lock.json

Records resolved package versions

↓
npm ci

Installs from the lockfile in a clean environment

In continuous integration and deployment environments, npm ci is commonly used when a compatible package-lock.json is committed with the project.

Terminal
npm ci

This command removes the existing node_modules directory before installing the dependency tree recorded in the lockfile. It expects a compatible lockfile and package manifest.

12. Common Versioning Mistakes

Mistake Why It Matters Better Approach
Assuming every package follows SemVer perfectly A release may still introduce unexpected changes. Read release notes and test upgrades.
Assuming ^ means any future version It generally restricts upgrades to compatible major versions. Understand the range before updating.
Deleting package-lock.json unnecessarily Resolution may change on the next installation. Keep the lockfile under version control.
Updating production dependencies without testing Application behavior may change. Run automated tests before deployment.
Confusing the project version with a dependency version They represent different things. Check the appropriate package.json property.

13. Practice Tasks

  1. Explain the difference between MAJOR, MINOR, and PATCH.
  2. Identify the parts of version 2.5.7.
  3. Explain the range allowed by ^1.2.3.
  4. Explain the range allowed by ~1.2.3.
  5. Install Express at a specific version.
  6. Use npm list --depth=0 to inspect installed packages.
  7. Use npm outdated to inspect update information.
  8. Explain the difference between package.json and package-lock.json.

Summary

Package versioning identifies different releases of a software package. Semantic Versioning uses MAJOR, MINOR, and PATCH numbers to communicate incompatible changes, backward-compatible features, and bug fixes. NPM supports version ranges such as the caret and tilde symbols to control compatible updates. Commands such as npm list, npm outdated, npm update, and npm ci help developers inspect, update, and install package versions. Understanding package versioning helps maintain stable and reliable Node.js applications at CIIT TRAINING INSTITUTE.