Discover Node.js
Learning Objectives
- Understand Node.js and its purpose
- Learn about modules and packages
- Master npm (Node Package Manager)
- Create reusable code modules
- Build applications with Node.js
Section 25.1: Introducing Node.js
What is Node.js?
JavaScript runtime for executing code outside the browser (servers, CLIs, tools).
Why Node.js?
- Server-side JavaScript
- Non-blocking I/O
- Fast execution
- Large ecosystem (npm)
- Same language everywhere (JavaScript)
Installing Node.js
# Check Node and npm versions
node --version
npm --version
# Run JavaScript file
node filename.js
Your First Node.js Program
// hello.js
console.log("Hello from Node.js!");
let message = "Welcome to server-side JavaScript";
console.log(message);
// Math operations work the same
console.log(2 + 3); // 5
Section 25.2: Node.js Modules
Built-in Modules
// File system
const fs = require("fs");
let content = fs.readFileSync("file.txt", "utf8");
// Path utilities
const path = require("path");
let filename = path.basename("/path/to/file.txt");
// Operating system info
const os = require("os");
console.log(os.platform()); // "linux"
// URL parsing
const url = require("url");
let parsed = url.parse("https://example.com?id=1");
Creating a Module
// math.js
function add(a, b) {
return a + b;
}
function subtract(a, b) {
return a - b;
}
module.exports = {
add: add,
subtract: subtract
};
Using Your Module
// app.js
const math = require("./math");
console.log(math.add(5, 3)); // 8
console.log(math.subtract(10, 4)); // 6
Section 25.3: Exporting a Class or Object
Exporting a Class
// Dog.js
class Dog {
constructor(name, breed) {
this.name = name;
this.breed = breed;
}
bark() {
console.log(this.name + " barks!");
}
}
module.exports = Dog;
Using the Class
// app.js
const Dog = require("./Dog");
let myDog = new Dog("Rex", "Golden Retriever");
myDog.bark(); // Rex barks!
Exporting Multiple Items
// utils.js
const formatDate = (date) => new Date(date).toISOString();
const formatCurrency = (amount) => "quot; + amount.toFixed(2);
const capitalize = (str) => str.charAt(0).toUpperCase() + str.slice(1);
module.exports = {
formatDate,
formatCurrency,
capitalize
};
Section 25.4: Node.js Packages
npm (Node Package Manager)
# Initialize new project
npm init -y
# Install a package
npm install express
npm install --save-dev nodemon
# List installed packages
npm list
# Update packages
npm update
# Remove package
npm uninstall express
package.json
{
"name": "my-app",
"version": "1.0.0",
"description": "My Node.js application",
"main": "index.js",
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js"
},
"dependencies": {
"express": "^4.17.1",
"axios": "^0.21.1"
},
"devDependencies": {
"nodemon": "^2.0.7"
}
}
Using Installed Packages
// Using express
const express = require("express");
const app = express();
app.get("/", (req, res) => {
res.send("Hello from Express!");
});
app.listen(3000, () => {
console.log("Server running on port 3000");
});
Section 25.5: Package Management with NPM
Common npm Commands
# Save to dependencies
npm install express
# Save to dev dependencies
npm install --save-dev jest
# Install from package.json
npm install
# Global installation
npm install -g nodemon
# Update all packages
npm update
# Uninstall package
npm uninstall express
Coding Challenges
Challenge 25.1: Circles Again
Task: Create a Circle module with methods
Challenge 25.2: Accounting Module
Task: Create accounting utilities module
Challenge 25.3: Playing with Dates
Task: Create date formatting utilities
Key Takeaways
✅ Node.js runs JavaScript on servers
✅ Modules are reusable code files
✅ module.exports shares code
✅ require() imports modules
✅ npm manages dependencies
✅ package.json tracks project info
Quiz Questions
- What is Node.js?
- How do you export a module?
- How do you import a module?
- What is npm?
- What does package.json contain?
Next Module: Module 26 - Create a Web Server