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

Initializing an NPM Project

Initializing an NPM project is the first step when creating a Node.js application. NPM helps us manage project information, install packages, and execute commands.

In this lesson, we will learn how to create a project folder, initialize NPM, understand the generated package.json file, install packages, and run a simple Node.js application.


1. What Does Initializing a Project Mean?

Initializing a project means preparing a folder to work as a Node.js project. During initialization, NPM creates a package.json file that stores project details and configuration.

For example, suppose we want to create a student management application.

Student Management Project
↓
Create Project Folder
↓
Run npm init
↓
package.json Created

2. Prerequisites

Before initializing a project, Node.js and NPM should be installed on your computer. NPM is normally included with Node.js.

Open a terminal or command prompt and execute these commands:

Terminal
node -v
npm -v

Example output:

v22.x.x
10.x.x

The exact version numbers may differ depending on the installed Node.js and NPM versions.

3. Create a Project Folder

First, create a folder for your application. We can create the folder using File Explorer or directly from the terminal.

Terminal
mkdir StudentManagement
cd StudentManagement

Explanation:

Command Purpose
mkdir StudentManagement Creates a new project folder.
cd StudentManagement Moves into the project folder.
Important: Run the following NPM commands from inside your project folder so that the files are created in the correct location.

4. Initialize NPM Using npm init

The npm init command starts an interactive process. NPM asks questions about the project, such as its name, version, description, entry point, and license.

Terminal
npm init

Example of the information NPM may request:

Terminal
package name: (studentmanagement) student-management
version: (1.0.0) 1.0.0
description: Student Management Application
entry point: (index.js) app.js
test command:
git repository:
keywords:
author:
license: (ISC) ISC

After you confirm the information, NPM creates the package.json file.

Using npm init -y

If you do not want to answer each question, use npm init -y. It creates a package.json file using default values.

Terminal
npm init -y
Remember: npm init asks questions. npm init -y accepts the default values automatically.

5. Understand the package.json File

The package.json file contains project metadata, dependencies, and scripts. It helps NPM understand how your application is configured.

package.json
{
  "name": "student-management",
  "version": "1.0.0",
  "description": "Student Management Application",
  "main": "app.js",
  "scripts": {
    "start": "node app.js"
  },
  "author": "",
  "license": "ISC"
}
Property Meaning
name The project name.
version The current project version.
description A short explanation of the project.
main The package entry-point field; its meaning depends on how the package is used.
scripts Defines commands that can be executed using NPM.
dependencies Lists packages required at runtime.
devDependencies Lists packages used during development.

6. Create Your First Node.js File

After initializing NPM, create an app.js file inside the project folder.

app.js
console.log("Welcome to Student Management!");

const studentName = "Rahul";
const course = "Node.js";

console.log("Student Name:", studentName);
console.log("Course:", course);

Output:

Welcome to Student Management!
Student Name: Rahul
Course: Node.js

The console.log() function displays messages in the terminal. This example confirms that your Node.js file can execute.

7. Run the Node.js Application

You can run the application directly by using the Node.js command.

Terminal
node app.js

You can also add a start script to your package.json file:

package.json
{
  "scripts": {
    "start": "node app.js"
  }
}

Now run the application using:

Terminal
npm start

Output:

Welcome to Student Management!
Student Name: Rahul
Course: Node.js
Note: If you replace the entire scripts object, make sure you retain any other scripts your project needs. The JSON file must also remain valid.

8. Install a Package

NPM allows us to install reusable packages instead of writing every feature from scratch. For example, Express is commonly used to build web applications and APIs with Node.js.

Terminal
npm install express

After a successful installation, the project may contain:

StudentManagement/
📁 node_modules/
📄 package.json
📄 package-lock.json
📄 app.js

node_modules contains installed packages, package.json records project configuration and dependency declarations, and package-lock.json records the resolved dependency tree for reproducible installs.

9. Common NPM Commands

Command Purpose
npm init Initializes a project interactively.
npm init -y Initializes a project with default values.
npm install Installs the dependencies declared by the project.
npm install express Installs Express and records it as a dependency.
npm start Runs the configured start script, if present.
npm list Displays the installed dependency tree.

10. Practice Task

Task: Create a Book Management project.

  1. Create a folder named BookManagement.
  2. Open the folder in your terminal.
  3. Run npm init -y.
  4. Create an app.js file.
  5. Print the book name, author, and price using console.log().
  6. Add a start script in package.json.
  7. Run the program using npm start.

Expected result: The terminal should display the book details entered in your program.

Summary

Initializing an NPM project prepares a folder for Node.js development. The npm init command creates a package.json file interactively, while npm init -y creates it using default values. The package.json file stores project information, scripts, and dependencies.

We can create an app.js file, execute it using node app.js, configure an NPM start script, and install packages using npm install. These are the basic steps for setting up a Node.js project.