Installation of Node.js
- Download the Installer
- Run the Installer
- Verify Installation
Installation of Node.js
1. Download the Installer
- Visit the official website: www.nodejs.org
- Choose the correct installer based on your operating system:
- Windows:
.msi file
- Linux: Binary code file
- macOS:
.pkg file
2. Run the Installer
- Double-click the downloaded file.
- Follow on-screen instructions.
- Windows: Accept default settings (includes adding Node.js to
PATH).
- macOS: Authenticate with password if prompted.
3. Verify Installation
- Open Command Prompt (Windows) or Terminal (macOS/Linux).
- Check Node.js version:
node -v
- Check npm version:
npm -v
- Note:
Node.js and npm Overview
- Node.js: JavaScript runtime for building server-side applications.
- npm (Node Package Manager):
- Package library for sharing, reusing, and installing modules.
- Example module: Mongoose – connects MongoDB with a data model.
- package.json: Defines project dependencies.
Node Package Registry
Creating a Node.js Packaged Module
Example: censorify Module
1. Create Project Folder
/censorify
2. Create Module File
censortext.js
3. Create package.json
- Minimum required fields:
- Example:
{
"author": "Brad Dayley",
"name": "censorify",
"version": "0.1.1",
"description": "Censors words out of text",
"main": "censortext",
"repository": { "type": "git", "url": "Enter your github url" },
"keywords": ["censor", "words"],
"dependencies": {},
"engines": { "node": "*" }
}
4. Create README.md
5. Build Local Package
npm pack
censorify-0.1.1.tgz
Publishing a Node.js Packaged Module
- Create a public repository (e.g., GitHub).
- Push
/censorify contents to the repository.
- Create an npm account: https://npmjs.org/signup
- Add npm user:
npm adduser
- Update
package.json with repository info and keywords.
- Publish module:
npm publish censorify
- Unpublish module:
npm unpublish censorify --force
Creating a Simple Node.js Application
1. Import Required Module
var http = require("http");
2. Create Server
http.createServer(function (request, response) {
response.writeHead(200, { 'Content-Type': 'text/plain' });
response.end('Hello World\n');
}).listen(8081);
console.log('Server running at http://127.0.0.1:8081/');
- Port: 8081
- Response: "Hello World"
Node.js Application Components
- Import Modules – Use
require.
- Create Server – Handle requests and responses.
- Read Request / Send Response – Process client input.
Summary of npm Commands
- Search:
npm search <module>
- Install:
npm install <module>
- Install globally:
npm install <module> -g
- View module info:
npm view <module>
- Remove module:
npm remove <module>
- Install specific version:
npm install <module>@<version>
Chatgpt