Installation of node js

Installation of Node.js

  1. Download the Installer
  2.  Run the Installer
  3.  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 0.6 and above include npm by default.
    • Node.js does not automatically update npm.
    • To update npm:
      npm install -g npm@latest
      

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

  • Registry located at https://npmjs.com
  • Allows publishing, searching, and installing packages.
  • Command line options:
    npm search express
    npm install
    npm install express
    npm install express -g
    npm view express
    npm remove express
    npm install express@4.14.0
    npm search openssl
    
  • All node modules include a package.json in the root directory.

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:
    • name
    • version
    • main
  • 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

  • Add usage instructions.

5. Build Local Package

npm pack
  • Generates:
censorify-0.1.1.tgz

Publishing a Node.js Packaged Module

  1. Create a public repository (e.g., GitHub).
  2. Push /censorify contents to the repository.
  3. Create an npm account: https://npmjs.org/signup
  4. Add npm user:
npm adduser
  1. Update package.json with repository info and keywords.
  2. Publish module:
npm publish censorify
  1. 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

  1. Import Modules – Use require.
  2. Create Server – Handle requests and responses.
  3. 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