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

NPM Introduction

NPM stands for Node Package Manager. It is the default package manager for Node.js and helps developers install, manage, and share reusable JavaScript packages in their applications.

NPM makes application development easier by providing access to thousands of packages created by developers. These packages can help with tasks such as handling HTTP requests, validating data, building APIs, and managing application functionality.

Learning objective: Understand what NPM is, why it is used, how packages work, and how to check NPM and Node.js versions.

1. What Is NPM?

NPM is a package management tool commonly used in Node.js projects. A package is reusable code that provides particular functionality. Instead of writing everything from scratch, developers can install packages and use them in their projects.

For example, a developer building a web application can use packages to create a server, handle routes, validate user input, or work with files.

Node.js Application

Your application code

↓
NPM

Installs and manages packages

↓
Packages

Reusable code used by your application

2. Why Do We Use NPM?

NPM helps developers perform several important tasks:

  • Install reusable JavaScript packages.
  • Manage project dependencies.
  • Record package information in package.json.
  • Run application commands through NPM scripts.
  • Install specific package versions.
  • Share and reuse packages across projects.
Simple example: Imagine you need to build a web server. You could write all the server functionality yourself, or install a package that provides useful server features. NPM helps you install and manage that package.

3. Node.js and NPM

Node.js provides the JavaScript runtime that executes JavaScript outside a browser. NPM helps manage the packages used by Node.js applications.

Node.js NPM
Runs JavaScript code outside the browser. Installs and manages packages.
Provides runtime capabilities. Helps manage dependencies and scripts.
Example command: node app.js Example command: npm install

4. Check Node.js and NPM Versions

Before starting a Node.js project, check whether Node.js and NPM are available on your computer.

Check the Node.js version

Terminal
node -v

Example output:

Output
v22.0.0

Check the NPM version

Terminal
npm -v

Example output:

Output
10.5.0

These are example version numbers. The actual output depends on the versions installed on your computer.

5. What Is an NPM Package?

An NPM package is a collection of reusable code and related files that can be installed and used in a project.

For example, Express is a popular package used to build web servers and APIs with Node.js.

Terminal
npm install express

This command installs the Express package in the current project.

After installation, a project may contain files like these:

MyProject/
|
|-- node_modules/
|-- package.json
|-- package-lock.json
|-- app.js
  • node_modules contains installed package files.
  • package.json describes the project and its dependencies.
  • package-lock.json records the resolved dependency versions.
  • app.js contains your application code.

6. Common NPM Commands

Command Purpose
npm -v Displays the NPM version.
npm init Starts an interactive project setup.
npm init -y Creates a default package.json file.
npm install Installs dependencies listed in the project manifest.
npm install express Installs Express as a project dependency.
npm uninstall express Removes Express from the project dependencies.
npm list Displays the installed dependency tree.

7. Create Your First NPM Project

Follow these steps to create a basic NPM project.

Step 1: Create a project folder

Terminal
mkdir MyNpmProject
cd MyNpmProject

Step 2: Initialize the project

Terminal
npm init -y

This command creates a package.json file with default project information.

Step 3: Create app.js

JavaScript
console.log("Welcome to NPM!");

console.log("My first NPM project is running.");

Step 4: Run the application

Terminal
node app.js

Expected output:

Output
Welcome to NPM!
My first NPM project is running.

8. NPM Package Management Flow

Create a Node.js Project
↓
Initialize NPM

npm init -y

↓
Install a Package

npm install package-name

↓
Manage Dependencies

package.json and package-lock.json

↓
Develop and Run the Application

9. Practice Tasks

  1. Check your installed Node.js version.
  2. Check your installed NPM version.
  3. Create a project folder named StudentApp.
  4. Initialize the project using npm init -y.
  5. Create app.js and print a welcome message.
  6. Explore the package.json file created by NPM.

Summary

NPM stands for Node Package Manager and helps developers install and manage reusable packages in Node.js applications. Commands such as npm init, npm install, and npm uninstall simplify project setup and dependency management. The package.json file stores project information and dependency details, while package-lock.json records resolved dependency versions. Understanding NPM provides a strong foundation for building and managing real-world Node.js applications at CIIT TRAINING INSTITUTE.