Useful npm packages to kickstart your Node project
4 min read
I’d like to share some useful and powerful npm packages you should take into consideration when thinking of kickstarting a new Node project (or probably when thinking of refactoring your legacy Node project 😃).
The packages I will suggest are entirely based on my experience when working with and researching Node.js.
You may build a RESTful web service or a microservice, background worker,… in your system. No matter what it is, you will always need these packages
winston for logging. Winston supports many types of logging from log format, pretty JSON display to output format such as STDOUT, syslog and raw file.
dotenv for loading environment variables from .env files.
eslint and prettier for code linting and code format. These two are like a couple and they are considered must-have packages for all Node projects from small to large. They work well with popular IDE/text editors like VS Code, Webstorm,…
Jest for unit test framework. You need a good tool to quickly write and run your unit test and Jest is one of the best out there ;)
nodemon as a great file watcher and auto-reload your Node program. Only use this one in development environment, you don’t want to apply this to production environment😈.
For production environment, you need to apply pm2: a process manager capable of running your Node program with monitoring, log management, auto-restart on failure and even clustering mode to speed up.
Lodash is very powerful and it provides many functions to simplify your code. BUT keep in mind that lodash package is heavy and you should not overuse it. Some functions may already exist in Node core libraries
If you need a REST service with public-facing APIs or internal microservice exposing REST APIs, you will need:
Express.js: the most popular Node web framework. It support REST APIs, old-school web app with server-side rendering and middleware to customize your APIs. Express comes with rich ecosystem with many supported packages like cors, express-winston and especially helmet, which is crucial to secure your APIs.
Fastify: a kinda new web framework but it’s gaining popularity due to faster speed compared to Express. If you aren’t convinced yet, you can see this post for some benchmarking results, quite superior 😄. I’d recommend Fastify if your service will have lots of requests per second.
For a well-structured and pre-defined structure (like Ruby on Rails) you should think of Nest.js as a choice for web framework. Express and Fastify are more free-style 😅
Apollo GraphQL ecosystem is a great tool for building GraphQL backend. You may need this to reduce API endpoint maintenance effort with built-in input/schema validation and ability to ask for only the data you need. I’d go for this when building public-facing backend-for-frontend (BFF). For basic intro to GraphQL, check this link out.
Yup for object schema validation. You will need a tool to help you validate payload, query params or headers of a REST API request to ensure it has correct structure you want. Yup also supports datatype and format validation such as email, positive/negative number and url.
An ORM (object-relation mapping) would be ideal for managing data model and queries to your relational database (MySQL, Postgres, MariaDB,…). You can either use TypeORM with many features to abstract from raw SQL commands or use a more native one with better performance like Knex.js. The choice is yours to choose an ideal one 😉
If communication to Elasticsearch is required, don’t forget elasticsearch package, along with elastic-builder for body builder for any CRUD operations
aws-sdk is the must-have for cloud application running on AWS to connect and interact with AWS services (most common one is AWS S3).
For building microservice mainly used for consuming events in event-driven architecture, you may need to evaluate the broker service you will use to determine the suitable packages. I can share some below:
amqp for applying AMQP protocol, which can easily communicate with RabbitMQ message broker. You can use for publish-subscribe event or work queue with it.
If you work with Kafka, kafkajs is the top of the list I must say. It can product and consume Kafka messages. If you work with Confluent, you may need to include this package to communicate and validate your Kafka message with Confluent Schema Registry
axios is perhaps one of the most used packages, not just for backend but even for frontend as well. You can use axios to make http request to other microservices for synchronous operations
There are so many good npm packages out there and these are just some of the best I have learned so far. Keep exploring and experimenting, guys and gals ;)
This post comes from original post from my Medium: dienbui.medium.com/useful-npm-packages-to-k..
Cover image is from https://developers.redhat.com/sites/default/files/styles/article_feature/public/nodejs-reference-architecture1x.png?itok=QbpLsgj