Node.js NPM

Node.js NPM

NPM is a package manager for Node.js packages, or modules.

NPM is used to download and install Node.js packages, or modules into your system.

NPM helps install, manage, and share JavaScript libraries and tools.

1.Initializing a New Project

mkdir node-website
cd node-website
npm init

It will ask for details like name, version, description, etc. You can skip all by running:

npm init -y

This creates a package.json file.

2. Installing Packages

npm install express

OR

npm i express

3. Install a package globally 

npm install -g nodemon

Global packages are installed system-wide and can be used from any project.

4. Uninstalling Packages

npm uninstall express

npm uninstall -g nodemon

5. View installed packages

npm list

6. View outdated packages 

npm outdated

7. Update all packages

npm update

8.  Install dependencies from package.json

npm install

9.  Running Scripts with NPM

Inside package.json, add: json
"scripts": {
  "start": "node server.js",
  "dev": "nodemon server.js"
}
 

Run the script:

npm run start
npm run dev


Below NPM command is used to install express package.

D:\nodejs\node-website> npm install express

Below NPM command is used to install moment package.

D:\nodejs\node-website> npm install moment

Below NPM command is used to install path package.

D:\nodejs\node-website> npm install path

Below NPM command is used to install async package.

D:\nodejs\node-website> npm install async

Below NPM command is used to install mysql package.

D:\nodejs\node-website> npm install mysql

             
Example  for express package: 

const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.send('Hello, World!');
});

app.get('/aboutus', (req, res) => {
   res.send('About us!');
});

app.listen(3000, () => {
  console.log('Server running on http://localhost:3000');
});

Example for moment package :

const moment = require('moment');

// Get current date and time

console.log("Current Date & Time:", moment().format('YYYY-MM-DD HH:mm:ss'));

// Format a date

const myDate = moment('2025-02-18');

console.log("Formatted Date:", myDate.format('MMMM Do YYYY, h:mm:ss a'));  

// Add days to the current date

console.log("Date after 5 days:", moment().add(5, 'days').format('YYYY-MM-DD'));

// Subtract months from the current date

console.log("Date 2 months ago:", moment().subtract(2, 'months').format('YYYY-MM-DD'));

// Get the day of the week

console.log("Day of the week:", moment().format('dddd'));  

// Get time from now

console.log("Time from now:", moment("2024-12-31").fromNow());  

// Check if a date is valid

console.log("Is the date valid?", moment("2024-02-30").isValid()); 


Output : 

Current Date & Time: 2025-02-17 13:00:47
Formatted Date: February 17th 2025, 12:00:00 am
Date after 5 days: 2025-02-23
Date 2 months ago: 2024-12-18
Day of the week: Tuesday
Time from now: 2 months ago
Is the date valid? false


Example  for path package:

const path = require('path');

// Get file path dynamically

const filePath = path.join(__dirname, 'data', 'example.txt');

console.log("Current Directory:", __dirname);

console.log("File Path:", filePath);

console.log("File Extension:", path.extname(filePath));

console.log("File Name:", path.basename(filePath));

console.log("Directory Name:", path.dirname(filePath));

Output : 

Current Directory: D:\xampp\htdocs\nodejs_tuttorials
File Path: D:\xampp\htdocs\nodejs_tuttorials\data\example.txt
File Extension: .txt
File Name: example.txt
Directory Name: D:\xampp\htdocs\nodejs_tuttorials\data

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

89662