Without any further ado, let's get our hand dirty with the Node.js.
Installation
Step 1: Download Node.js
Visit the official Node.js website:
Download the LTS (Long-Term Support) version, which is recommended for most users.
Step 2: Verify Installation
Open a terminal or command prompt and run:
node -v
OR
node --versionBoth are same.
Example output:
v24.16.0Check npm (Node Package Manager):
npm -v
OR
npm --versionBoth works.
Example output:
11.13.0Understanding the Node.js Distribution
When you install Node.js, you actually receive several important tools.
The installation package includes:
Node.js Runtime
npm (Node Package Manager)
Core Node Modules
Development ToolsLet's understand each component.
Node.js Runtime:
The runtime executes JavaScript code outside the browser.
Example:
console.log("Hi");
Without Node.js:
Runs inside browser only
With Node.js
Runs directly on your computer
npm (Node Package Manager)
npm is the world's largest software package registry.
It allows developers to:
- Install libraries
- Manage dependencies
- Share packages
- Automate development tasks
Example:
npm install express
This command downloads and installs the Express framework.
Today, millions of packages are available through npm.
Your First Node.js Program
Create a file named:
app.js:
Add the following code:
console.log("Hello, Node.js");Run it using:
node app.jsOutput:
Hello, Node.jsYippeee, you just created your first Node.js program.
Understanding npm
npm (Node Package Manager) comes installed with Node.js and allows you to install libraries and tools.
Initialize a new project:
mkdir my-node-app
cd my-node-app
npm init -yThis creates a file named:
package.jsonThe package.json file stores project information and dependencies.
Installing Packages
Install a package using npm.
Example:
npm install lodashThis downloads the package and adds it to:
node_modules/You can now use it in your code:
const __ = require("lodash");
console.log(__.capitalize("welcome to nodejs"));Output:
Welcome to nodejsWorking with Modules
Node.js uses modules to organize code.
math.js:
function add(a, b) {
return a + b;
}
module.exports = add;app.js:
const add = require("./math");
console.log(add(5, 3));Output:
8Modules help keep applications clean and maintainable.
Node Version Manager (NVM)
As a developer, you will often work on multiple projects.
Project A: Node 20
Project B: Node 22Installing and uninstalling versions repeatedly becomes difficult.
This problem is solved using NVM.
What is NVM?
NVM stands for:
Node Version Manager.
It allows you to:
- Install multiple Node versions
- Switch between versions
- Test compatibility
Example:
nvm install 20nvm install 22Switch:
nvm use 22Check:
node -vOutput:
v22.x.xCreate a Simple HTTP Server:
One of Node.js's most common uses is building web servers.
Save the following code in a file named app.js:
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello, Node.js!\n');
});
server.listen(3000, () => {
console.log('Server running at http://localhost:3000/');
});
Run the Application:
Open the terminal, navigate to your project directory, and execute:
node app.jsView in Browser:
Open your browser and visit: http://localhost:3000 to see the Node.js application in action.
Understanding Asynchronous Programming
Node.js is famous for its asynchronous nature.
Example:
console.log("Start");
setTimeout(() => {
console.log("Processing..");
}, 2000);
console.log("End");Output:
Start
End
ProcessingUnderstanding the Node.js Runtime
When you run:
node app.jsNode.js:
- Reads the JavaScript file.
- Executes the code using the V8 engine.
- Code compiles to machine code.
- Runtime executes instructions.
- Provides additional APIs such as:
- File System
- HTTP Server
- Networking
- Process Management
- Streams
- Output appears
These APIs are not part of JavaScript itself but are provided by Node.js.
REPL (Read-Eval-Print Loop)
Node.js includes an interactive environment called REPL.
Start it:
nodeExample:
> 5 + 10
15
> "Hello".toUpperCase()
HELLOREPL is useful for testing small code snippets and learning JavaScript concepts.
Modules in Node.js
Node.js applications are divided into modules.
A module is simply a file containing reusable code.
math.js
function add(a, b) {
return a + b;
}
module.exports = add;app.js
const add = require("./math");
console.log(add(5, 4));Output:
8Benefits:
- Better organization
- Reusable code
- Easier maintenance
CommonJS Module System
Node.js traditionally use CommonJS.
Export:
module.exports = myFunction;Import:
const myFunction = require("./myFile");Modern Node.js also supports ES Modules:
export function add() {}
import { add } from "./math.js";Events in Node.js
Node.js follows an event-driven architecture.
Using EventEmitter:
Leave a comment
Your email address will not be published. Required fields are marked *
