What is the NPM package?

Npm (Node Package Manager) is libraries or modules written in javascript. NPM allow us to share and reuse a piece of code through the Node js ecosystem.

In this blog we gonna walkthrough widely using NPM packages in a nutshell. Play around with these packages based on your requirements.

Widely using npm packages in express JS project

ExpressJS

Express js itself is a standalone npm package. It’s a lightweight framework for creating faster, scalable web applications.


npm install express

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

// Your routes and other middleware can come after this

// Start the server
app.listen(3000, () => {
  console.log('Server started on port 3000');
});

 

Body-parser

Body-parser enables us to ****parse the request body and extract data from the backend.

npm install body-parser

// Middleware for parsing JSON and URL-encoded form data
app.use(bodyParser.json());

app.use(bodyParser.urlencoded({ extended: true }));

 

Morgan

It’s a middleware function that enables the log of the HTTP request information.

npm install morgan

// Middleware for logging requests
app.use(morgan('dev'));

// HTTP request information in console
GET /api/users 200 10.899 ms - 45

 

CORS

By using CORS (Cross-Origin Resource Sharing) we can control which origin can use the backend endpoints

npm install cors

const cors = require('cors');

// Middleware for enabling CORS
app.use(cors());

 

Passport

If you want to enable local/third-party authentication the passport package makes your life easier.

npm install passport passport-local

For the sake of simplicity, we use passport-local here.


const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;

// Configure Passport with a local authentication strategy
passport.use(new LocalStrategy(
  function(username, password, done) {
    // Code to check if the username and password are valid
    if (username === 'admin' && password === 'password') {
      return done(null, { id: 1, username: 'admin' });
    } else {
      return done(null, false);
    }
  }
));

// Initialize Passport and restore authentication state, if any, from the session
app.use(passport.initialize());
app.use(passport.session());

 

Nodemon

If you want to see the changes in the express application, you need to stop the server and run it again.

Nodemon gets rid of manual restarts by handling automatic server restarts.

npm install nodemon --save-dev

In package.json file

"scripts": {
  "start": "node app.js",
  "dev": "nodemon app.js"
}

finally, run the server by using this start script.

npm run dev

 

Dotenv

Dotenv enables you to get process sensitive information without explicitly showing the credentials through the app.

npm install dotenv

In .env file

MY_SECRET_KEY=abcd1234
DB_USERNAME=myuser
DB_PASSWORD=mypassword

const dotenv = require('dotenv');

dotenv.config(); // Load environment variables from .env file

 

Conclusion

The above list of NPM packages is mostly used npm packages in the industry. There are millions of npm packages out there on the internet.

Make sure you pick the right package by its weekly downloaded numbers and its dev support community and also follow its official documentation to ensure efficient use of that package.

Happy learning/coding 😎