Jigar's blog

Essential Node.js Packages for Every Project

Published on
Published on
/
3 mins read
/
––– views

Every Node.js project can benefit from a set of standard packages and configurations that enhance productivity, maintainability, and performance. Here’s a curated list of packages and tools you should consider for every project.

Packages to Install

Development Essentials

  • dotenv: Manage environment variables with ease.

    npm install dotenv
    

    Usage:

    require('dotenv').config();
    console.log(process.env.MY_VARIABLE);
    
  • nodemon: Automatically restart your application on file changes during development.

    npm install --save-dev nodemon
    

    Add a script to package.json:

    "scripts": {
      "start:dev": "nodemon index.js"
    }
    
  • eslint: Enforce consistent code style and catch potential errors.

    npm install --save-dev eslint
    

    Initialize ESLint with:

    npx eslint --init
    
  • prettier: Automatically format your code for better readability.

    npm install --save-dev prettier
    

    Integrate with ESLint:

    npm install --save-dev eslint-config-prettier eslint-plugin-prettier
    
  • husky & lint-staged: Automate code quality checks before commits.

    npm install --save-dev husky lint-staged
    

    Add hooks in package.json:

    "husky": {
      "hooks": {
        "pre-commit": "lint-staged"
      }
    },
    "lint-staged": {
      "*.js": ["eslint --fix", "prettier --write"]
    }
    

Utility Packages

  • axios: Simplify HTTP requests.

    npm install axios
    
  • dayjs: Lightweight and fast library for date manipulation.

    npm install dayjs
    
  • uuid: Generate unique IDs.

    npm install uuid
    
  • winston: A versatile logging library.

    npm install winston
    

    Example setup:

    const winston = require('winston');
    const logger = winston.createLogger({
      level: 'info',
      format: winston.format.json(),
      transports: [
        new winston.transports.Console(),
        new winston.transports.File({ filename: 'combined.log' })
      ]
    });
    logger.info('Hello, Winston!');
    

Security Packages

  • helmet: Enhance API security by setting various HTTP headers.

    npm install helmet
    

    Usage:

    const helmet = require('helmet');
    app.use(helmet());
    
  • cors: Enable Cross-Origin Resource Sharing.

    npm install cors
    

    Usage:

    const cors = require('cors');
    app.use(cors());
    
  • bcrypt: Handle password hashing securely.

    npm install bcrypt
    

Performance Packages

  • compression: Compress HTTP responses for faster client-side load times.

    npm install compression
    

    Usage:

    const compression = require('compression');
    app.use(compression());
    
  • pm2: Manage your application in production with clustering and monitoring.

    npm install -g pm2
    

    Start your app:

    pm2 start index.js
    

Testing Packages

  • jest: A powerful testing framework.

    npm install --save-dev jest
    
  • supertest: Test your APIs with ease.

    npm install --save-dev supertest
    

Optional Extras

  • zod or joi: Schema validation for request and response data.
    npm install zod
    

Boilerplate package.json Scripts

"scripts": {
  "start": "node index.js",
  "start:dev": "nodemon index.js",
  "lint": "eslint .",
  "format": "prettier --write .",
  "test": "jest"
}

Conclusion

Incorporating these packages and configurations will help streamline your Node.js project development process, making your codebase cleaner, more secure, and easier to maintain.

Discussion (0)

🚀 Join the conversation!
Log in with GitHub or Google to share your thoughts and connect with the community.