Which of the 635000 npm modules do I choose?
Tags: node, javascript
Published: 2018-11-14 00:21:36

---
title: Which of the 635000 npm modules do I choose?
published: true
canonical_url: https://www.coreycleary.me/which-of-the-635000-npm-modules-do-i-choose/
description: 
tags: node,javascript
---
<em><strong>Originally published at <a href="https://www.coreycleary.me/which-of-the-635000-npm-modules-do-i-choose/">coreycleary.me</a></strong>. This is a cross-post from my content blog. I publish new content every week or two, and you can <a href="https://www.coreycleary.me/about/">sign up to my newsletter</a> if you'd like to receive my articles directly to your inbox! I also regularly send cheatsheets, links to great tutorials by other developers, and other freebies!</em>

If you've spent any time in the Node or Front End JavaScript world, you know there are hundreds of thousands of modules to choose from.

Developers constantly ask things and express pain like:
<blockquote>"What's eating away at us is which modules to choose..."</blockquote>

<blockquote>"What is the difference between X module and Y module and which one is better?"</blockquote>

<blockquote>"npm is great but those modules might be useless in 6 months, a year or later depending on how they're supported."</blockquote>
Usually when asking such questions, you'll get ten different answers. And then everyone gives you their own favorite pet module and the rest of the thread is people arguing over which one is the best.

It's easy to face "analysis paralysis" when choosing npm modules. With so many to choose from, and new ones being touted as "what you really should switch to" it can be overwhelming to pick the right one for your project. And it doesn't help that so many of these modules do similar (or the same) things.

Rather than waste time Googling around, combing through <a href="http://npmjs.org">npmjs.org</a>, and wasting more time NOT building your application, it would be great to know <em>which</em> modules to choose <em>when</em>.

<h3>A curated list</h3>

In order to help combat this, below you'll find a list of modules for the most common types of problems (i.e. - web frameworks, templating, authentication, etc) and <i>when to use each one.</i>

There are a few caveats to this: you might be familiar with some or even many of these modules, but sometimes you're touching a part of the stack you haven't yet (maybe something like authentication or Web Sockets) and you need to know which modules will get the job done. You may have a module you think is better. You may have a use case/requirement not covered here. Rather than have a list of 10 different ones for the same category, I narrowed it down so that you can avoid the trap of analysis paralysis. By all means supplement this with research if you think you have a use case not covered, but this is designed to get you up and running much more quickly.

These modules were selected based on the following:
<ul>
 	<li>how well they get the job done</li>
 	<li>community size (important for support/troubleshooting)</li>
 	<li>active maintenance</li>
</ul>
If you find yourself still without enough information to make a decision, I'd suggest <a href="http://slant.co">slant.co</a> and <a href="http://nodejs.libhunt.com">nodejs.libhunt.com</a> to help with comparisons.

<em>Note: in order to keep the scope reasonable, these modules are with the server-side in mind. Many can be used on the client or on the server, but this was approached "server-first".</em>

<h3>Categories:</h3>
<a href="#http-requests">HTTP requests</a>
<a href="#web-frameworks">Web frameworks</a>
<a href="#validation">Validation</a>
<a href="#authentication">Authentication</a>
<a href="#asynchronous">Asynchronous</a>
<a href="#database">Database</a>
<a href="#process-management">Process management</a>
<a href="#web-sockets">Web Sockets</a>
<a href="#api-documentation">API documentation</a>
<a href="#utilities">Utilities/misc</a>
<a href="#cli">CLI</a>
<a href="#logging">Logging</a>
<a href="#templating">Templating</a>
<a href="#testing">Testing</a>
<a href="#tooling">Tooling</a>
<a href="#debugging">Debugging</a>
<h3 id="http-requests">HTTP requests</h3>
<ul>
 	<li><em>Request</em>:
<ul>
 	<li>use when you need to make callback-based HTTP requests, i.e. from one REST service to another.</li>
</ul>
</li>
 	<li><em>Axios</em>:
<ul>
 	<li>use when you need to make promise-based HTTP requests</li>
 	<li>note: you could use request-promise but axios has less dependencies and is built on native Promises</li>
</ul>
</li>
</ul>
<h3 id="web-frameworks">Web frameworks</h3>
<ul>
 	<li><em>Express</em>:
<ul>
 	<li>use when you want a lightweight web framework for your API's, website, or single page app</li>
 	<li>you don't mind using callbacks out-of-the-box for asynchronicity</li>
 	<li>you want to be able to choose from a wide ecosystem of modules that work with the framework</li>
 	<li>you want a large community for support and troubleshooting</li>
</ul>
</li>
 	<li><em>Koa</em>:
<ul>
 	<li>use when you want an even-more barebones framework than Express</li>
 	<li>Koa is more of a middleware layer and doesn't provide templating or routing OOTB, making it better suited for API development</li>
 	<li>you want async/await support OOTB</li>
</ul>
</li>
 	<li><em>Hapi</em>:
<ul>
 	<li>use when you want a framework which has more "batteries" than Express or Koa, but not as much baked-in as Sails</li>
</ul>
</li>
 	<li><em>Sails</em>:
<ul>
 	<li>use when you want something like Rails, something that has everything baked-in (but a lot of which you might not need depending on your application)</li>
</ul>
</li>
</ul>
<h3 id="validation">Validation:</h3>
<ul>
 	<li><em>Ajv</em>:
<ul>
 	<li>use when you need to validate JSON (like coming in from a web request)</li>
 	<li>you want to share these schemas with other non-JS parts of the application (since it's JSON, you can do that)</li>
</ul>
</li>
 	<li><em>Joi</em>:
<ul>
 	<li>use when you need to validate input and like the style of
```javascript
const schema = joi.object().keys({
  id: joi.string().guid().required(),
  username: joi.string().alphanum().min(8).required()
});
```
rather than defining schemas in JSON</li>
 	<li><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen-Sans, Ubuntu, Cantarell, 'Helvetica Neue', sans-serif;">you're using Hapi (Joi comes with it OOTB)</span></li>
</ul>
</li>
</ul>
<h3 id="authentication">Authentication:</h3>
<ul>
 	<li><em>Passport</em>:
<ul>
 	<li>use when you need authentication middleware for your website or API</li>
 	<li>you want to be able to choose between multiple authentication types (Oauth, Facebook, etc)</li>
 	<li>you need to manage sessions</li>
</ul>
</li>
</ul>
<h3 id="asynchronous">Asynchronous:</h3>
<ul>
 	<li><em>Async (library)</em>:
<ul>
 	<li>use when you need to support an older version of Node that only supports callbacks, not Promises</li>
</ul>
</li>
 	<li><em>ES6 native promises (native JS, not npm)</em>:
<ul>
 	<li>use when you are working with a version of Node greater than 0.12</li>
 	<li>another thing to consider is your team's level of comfort with Promises. In 2018, most developers should be.</li>
</ul>
</li>
 	<li><em>async/await (native JS, not npm</em>):
<ul>
 	<li>use when you escaped callback hell only to discover Promise hell</li>
 	<li>you've got lots of `.then` and `.catch` from Promises</li>
</ul>
</li>
</ul>
<h3 id="database">Database:</h3>
The below is a mix of database drivers, ORM's and a query builder. Before you reach for the ORM, I'd highly suggest making sure you need to use one in the first place. Often they add another layer of abstraction that does not necessarily provide enough pay off, when you could just use raw SQL or a query builder.
<ul>
 	<li><em>mysql, node-postgres</em>:
<ul>
 	<li>use when you don't need a full-on ORM, but instead are going to be querying your database with raw SQL (these are the drivers)</li>
</ul>
</li>
 	<li><em>node-mongodb-native</em>:
<ul>
 	<li>use when you don't need a full-on ORM, but instead are going to be querying MongoDB directly</li>
</ul>
</li>
 	<li><em>Mongoose</em>:
<ul>
 	<li>use when you would rather use an ORM for MongoDB</li>
</ul>
</li>
 	<li><em>Knex</em>:
<ul>
 	<li>use when you don't need a full ORM solution but instead just need something to make writing queries in code easier</li>
 	<li>Knex is a query builder which generates SQL</li>
 	<li>you've got a Postgres, MSSQL, MySQL, MariaDB, SQLite3, Oracle, or Amazon Redshift database</li>
</ul>
</li>
 	<li><em>Objection.js</em>:
<ul>
 	<li>you want an ORM that supports everything Knex supports, doesn't use a query DSL (so what you're writing is closer to raw SQL), has a Promise-based API and good documentation</li>
</ul>
</li>
</ul>
<h3 id="process-management">Process management:</h3>
A helpful resource in comparing some process managers is <a href="http://strong-pm.io/compare">http://strong-pm.io/compare</a>. <em>Note</em>: they also include StrongLoop Process Manager, which is good but a bit hefty. I'd recommend checking out the solutions before first before deciding you're going to reach for StrongLoop's.
<ul>
 	<li><em>PM2</em>:
<ul>
 	<li>use when you want a process manager that will handle restarting a service if it crashes and allow you to control clustering</li>
 	<li><em>note</em>: there is some haziness around potential violations of the AGPL license that PM2 is licensed under. There is some discussion on this <a href="https://opensource.stackexchange.com/questions/5179/can-one-use-agpl-application-without-copy-left-effects">here</a>. My take is it's most likely ok to use. but check with your legal department if you have questions/concerns as I'm not a lawyer.</li>
</ul>
</li>
 	<li><em>forever</em>:
<ul>
 	<li>use when you want a process manager that will handle restarting a service if it crashes</li>
 	<li>you've got a smaller deployment (pm2, with its clustering support, is for larger scale deployments). If you only have a handful of services/processes, you're probably fine using forever</li>
</ul>
</li>
 	<li><em>nodemon</em>:
<ul>
 	<li>use when you want to monitor for any code changes in your application and automatically restart the server when developing locally</li>
 	<li>great to use for development!</li>
</ul>
</li>
</ul>
<h3 id="web-sockets">Web Sockets:</h3>
For Web Sockets, rather than include a list of different ones I'm just recommending primus. It supports all the major Web Sockets implementations, is actively supported, and you can easily swap in and out different libraries with a line of code change if it turns out you need a different one.
<ul>
 	<li><em>Primus</em>:
<ul>
 	<li>use when you want Web Sockets but don't want to deal with being locked in to any particular Web Sockets implementation</li>
</ul>
</li>
</ul>
<h3 id="api-documentation">API documentation:</h3>
<ul>
 	<li><em>Swagger-node</em>:
<ul>
 	<li>use when you need to document your REST API and be able to test requests against endpoints</li>
</ul>
</li>
</ul>
<h3 id="utilities">Utilities/misc:</h3>
<ul>
 	<li><em>Lodash</em><em>:</em>
<ul>
 	<li>use when you need a JS utility library</li>
 	<li>you use a lot of OOP</li>
</ul>
</li>
 	<li><em>Ramda</em>:
<ul>
 	<li>use when you want to program in a more functional style using functional composition</li>
 	<li>you want something like lodash but in the functional paradigm</li>
</ul>
</li>
 	<li><em>Moment:</em>
<ul>
 	<li>use when you need to parse, validate, manipulate, and display dates/time</li>
</ul>
</li>
 	<li><em>UUID</em>:
<ul>
 	<li>use when you need random, unique id's that are hard to crack</li>
</ul>
</li>
 	<li><em>NVM:</em>
<ul>
 	<li>use when you want to be able to switch between multiple versions of Node installed in your environment</li>
</ul>
</li>
 	<li><em>Fs-extra</em>:
<ul>
 	<li>use when you need to be able to `mkdir` recursively, `rm -rf`, and other file system utilities that are missing in Node</li>
</ul>
</li>
 	<li><em>Nodemailer</em>:
<ul>
 	<li>use when you need to send emails from Node</li>
</ul>
</li>
 	<li><em>Dotenv</em>:
<ul>
 	<li>use when you need to load environment variables from a .env file into `process.env`</li>
</ul>
</li>
</ul>
<h3 id="cli">CLI:</h3>
<ul>
 	<li><em>Commander</em>:
<ul>
 	<li>use when you want to build a CLI utility that takes all the arguments as flags on the command line</li>
</ul>
</li>
 	<li><em>Inquirer</em>:
<ul>
 	<li>use when you want to build an "interactive" CLI utility that takes options sequentially (similar to how when you run `npm init` and it asks you a series of questions to generate the package.json file)</li>
</ul>
</li>
</ul>
<h3 id="logging">Logging:</h3>
<ul>
 	<li><em>Winston</em>:
<ul>
 	<li>use when you need a logging library and need different log outputs</li>
</ul>
</li>
 	<li><em>Bunyan</em>:
<ul>
 	<li>use when you need a logging library and can deal with JSON being the only log output</li>
 	<li>you want to have different loggers for different components, request, or functions (i.e. - these loggers might parse things differently)</li>
</ul>
</li>
 	<li><em>Morgan</em>:
<ul>
 	<li>use when you're using Express and you want to log your HTTP requests</li>
 	<li><em>note</em>: this would be used in conjunction with something like Winston or Bunyan. Since it's middleware, it know how to handle the request and log it, but doesn't handle the transportation to a log output that Winston and Bunyan do.</li>
</ul>
</li>
</ul>
<h3 id="templating">Templating:</h3>
<ul>
 	<li><em>Pug (formerly Jade)</em>:
<ul>
 	<li>use when you need a server-side templating engine that is easy to read and has support for blocks OOTB for sublayouts</li>
 	<li>your output is only HTML</li>
</ul>
</li>
 	<li><em>EJS</em>:
<ul>
 	<li>use when you need a server-side templating engine that fully uses JS and is forgiving of whitespace indentation (Pug is not)</li>
 	<li>note: does not support async JS functions though</li>
</ul>
</li>
</ul>
<h3 id="testing">Testing:</h3>
<ul>
 	<li><em>Mocha</em>:
<ul>
 	<li>use when you need to write and run unit tests</li>
</ul>
</li>
 	<li><em>Chai</em>:
<ul>
 	<li>use when you need a library to prove your assertions in your unit tests</li>
 	<li><em>note</em>: this would be used in conjunction with Mocha</li>
</ul>
</li>
 	<li><em>Chai-as-promised</em>:
<ul>
 	<li>use when you want to prove your assertions on promises rather than putting the assertion in the <code>then</code> or <code>catch</code></li>
</ul>
</li>
 	<li><em>Sinon</em>:
<ul>
 	<li>use when you need a mocking library for your tests</li>
</ul>
</li>
</ul>
<h3 id="tooling">Tooling:</h3>
<ul>
 	<li><em>ESdoc</em>:
<ul>
 	<li>use when you want to generate API documentation from your code and you're using newer versions of JS</li>
 	<li>supports current versions of JS - targets `class` by default, so if you're using prototypes in your code, use JSdoc</li>
</ul>
</li>
 	<li><em>JSdoc</em>:
<ul>
 	<li>use when you want a code API documentation generator that supports ES6</li>
 	<li>supports both classes and prototypes</li>
</ul>
</li>
 	<li><em>ESlint</em>:
<ul>
 	<li>use when you need a linter to automatically find (and fix) syntax and code pattern issues in your code</li>
</ul>
</li>
</ul>
<h3 id="debugging">Debugging:</h3>
Native Node debugging has enough baked in now that my recommendation is to use what's there. A couple years ago it was helpful to pull in some npm modules and you might have a specific use case that requires one, but now there's enough native support that it makes sense to leave out the extra dependencies if you're not doing anything too crazy with debugging.
<h3>Conclusion</h3>
Picking modules can be tough, but you just need some waypoints to figure it out. Use this guide to help you when you're wasting time deciding what to pick or don't even know where to start.
<h3>One last thing!</h3>

I'm writing a lot of new content to help make Node and JavaScript easier to understand. Easier, because I don't think it needs to be as complex as it is sometimes. If you enjoyed this post and found it helpful <a href="https://www.coreycleary.me/about/">here's that link again</a> to subscribe to my newsletter!