DEV Community

Cover image for You may not need to install that package.
Hosein Pouyanmehr
Hosein Pouyanmehr

Posted on • Updated on

You may not need to install that package.

What is this post about?

Installing a package in the project is always a concern for me. The more package you install, the more dependent your app will become. Also, when you have a lot of dependencies it can become hard and risky to update your app.
The whole purpose of this post (that is an intro of a series) is to give you some tips to help you decide better when it comes to installing a package in your app.

Is installing a package always the best way?

As you know, there are lots of packages out there. I can say for more than 95% of your needs, there is at least one package on NPM. To answer the question that I've asked as the title of this section, let's first take a look at some of the pros and cons of installing a package.

Pros

  • You will be faster.
  • You don't need to focus on creating tools. You can use them to achieve your app goals.
  • You can use the benefits of the quality and experience of the community behind the package.
  • You can have an updated approach to your problems.

Cons

  • Increase your app size. (More disk usage)
  • You need to learn the package architecture and sometimes read a lot of docs.
  • Some packages don't have complete docs or tutorials, so you may spend your time investigating how to use that package. (Like draft-js)
  • That can make you lazy in thinking and weaken your problem-solving ability.
  • Can increase the security vulnerabilities of your app.
  • Can lead to compatibility issues.
  • The app performance can be affected.

So, Is it always the best way to install the package? I say no. Using one or two utilities of a package may not always be the right choice. I agree with not reinventing the wheel, but in many cases, it's like buying a car and just using one of its wheels.

How to choose between using a package or self-made solutions?

Clarify your problem

To determine if you need a package (or not), first, clarify the problem or issue that you're facing. Don't be attracted to package utilities or download size at the first step. For example, if you need to clone an array of numbers and strings, there is no need to install lodash definitely.

Search for the solution rather than the tool

If your problem is clear, try to find a non-NPM solution, and don't jump into the NPM links. In most cases, there is a solution or a utility out there, and you can adaptively use that to your application.

Analyze your time

If you have a limited time and your problem can beat your time limits, you should probably install a package. But you can do it differently to replace it with your code later. Let's say you have to implement five complicated utilities for your project, but you don't have enough time, Install the package but don't import the utilities directly to your code. You can try to create a module for your need and use the package utilities just in that module. With this method, you somehow proxied your usage of that package, and by having a single source of truth (SSOT) you can implement your utilities whenever you have enough time.

A diagram of proper NPM package importing

On the other hand, if time isn't that limit for you and your problem isn't that big, You can keep thinking about installing the package or not.

Experience Vs Problem

Sometimes, it is neither about time nor your resources. When it comes to experiences, it can become obvious, that you can not solve your problem with your current knowledge. In this situation, you should focus on finding the most suitable NPM package for your need. Also, try to use it in the same method mentioned earlier, Don't import the package everywhere.

Conclusion

There could be other factors that can affect your decision, but generally, you should always take care of your application. Bundle size, app security, performance, and other application aspects. A lot of issues in the application can be fixed in a few lines of code. So take a deep look at each package you are going to use. It's nice to have a tidy package.json.


A banner of become a backer

Hi! I'm Hosein Pouyanmehr. I enjoy sharing what I learn and what I find interesting. Let's connect on LinkedIn.

See my code interests on GitHub.

Top comments (0)