20 modules for Node.js that you should know about

Original author: Philip Ackermann
  • Transfer
Philip Ackerman, the author of an article we are translating today, said that in recent years, the Node.js platform has become more and more popular. It is often used to create the server parts of web applications, as well as to solve other problems that arise during the software development process. Currently , there are more than half a million modules in the npm registry of the package manager for Node.js.

image

We present to your attention an overview of modules that the author of the material, a programmer who is involved in, among other things, also in web development, considers it useful. Among them are libraries for working with images, tools for checking what users enter into forms, modules for optimizing and minifying data of various types, tools for creating PDF files, for logging and developing command-line applications.

Graphics


▍1. Work with images


GraphicsMagick and ImageMagick are two popular image tools. They can be used from JavaScript-code through npm-module gm . In general, all this allows you to create, edit, combine images. For example - change their sizes, crop, perform format conversion.

Here's what working with this module looks like.

const gm = require('gm');
gm('/path/to/image.jpg')
  .resize(500, 250)
  .autoOrient()
  .write(response, error => {});

▍2. Image processing


The sharp module is based on the incredibly fast libvips image processing library. In the tasks of compressing images and resizing them, he, as they say on his page, is 4-5 times faster than ImageMagick or GraphicsMagick. The module supports the formats JPEG, PNG, WebP, TIFF, GIF and SVG; it can output images in the formats JPEG, PNG, WebP, or in the form of uncompressed streams of source pixel data.

▍3. Create sprite sheets


Sprite lists are graphic files that contain many small images (for example, icons). In our case, we are talking about the so-called CSS sprites. They are often used to facilitate the task of loading images, which has a beneficial effect on page output speed. Creating sprite sheets manually is an ungrateful task. Automate this process using the spritesmith module . Information about the folder with graphic files is transmitted to the input of this module, and it converts all these files into a single sprite list. In addition, it generates a JSON file with information about the coordinates of each image in the finished sprite sheet, which can be used in CSS.

Dates, Strings, Colors


▍4. Date formatting



Moment.js is a great alternative to the standard Date object.

The standard JavaScript API has a Date object designed to work with values ​​representing the date and time. However, this object is not particularly convenient in cases of output and formatting dates. In order to simplify the work with the date and time, you can use the Moment.js library . It has a clear, thoughtful interface, and the code that comes from its use is clear and readable.

moment()
  .add(7, 'days')
  .subtract(1, 'months')
  .year(2009)
  .hours(0)
  .minutes(0)
.seconds(0);

In addition, there is a plugin for this library, which is used to parse and format dates for various time zones.

▍5. Validating String Data


If you offer users of your site to fill out a form, do not forget to check the data they entered. To do this is not only on the client side, but also on the server - in order to filter out dangerous data sent by an attacker. The module, designed to solve string checking problems, validate.js , provides the developer with many useful methods, the purpose of which is clear from their names. For example, it is the methods isEmail(), isURL(), isMobilePhone(), isCreditCard(). You can use it both on the server and on the client.

▍6. Work with color values


Converting values ​​representing colors from one format to another is one of the tasks that periodically confronts a front-end developer. The TinyColor2 module simplifies the solution to this problem, it can be used both in the browser and in the Node.js. He gives to programmers a set of methods for converting color values, such as toHexString(), toRGBString()as well as methods to perform various operations with flowers. Among them - lighten(), saturate(), complement().

Work with data of various formats


▍ 7. Create PDFs


If you need to dynamically generate PDF files - pay attention to the PDFKit module . It supports embedding in documents fonts, images and descriptions of vector drawings, either generated programmatically (using an API similar to Canvas), or created in SVG format. Moreover, with its help you can create hyperlinks, include notes in files, select text and do much more useful things. Perhaps if you need something like this, getting started with PDFKit is worth exploring its interactive demo , which works in a browser.

▍8. HTML file processing



Cheerio makes it easier to parse HTML files on the server

If you find yourself in a situation where you had to parse an HTML file on the server and you lacked jQuery features, then you may well need to take a look at Cheerio . Although this module is just an implementation of a subset of the jQuery features, it makes it much easier to parse HTML files on the server. It is built on the basis of the htmlparser2 module (this is a parser for HTML, XML and RSS). In addition, taking into account the results of benchmarks, it is eight times faster than jsdom , another module that allows you to work with the DOM on the server.

▍ 9. CSV file processing



The node-csv module simplifies working with CSV data.

The CSV (comma-separated value) format is often used to organize the exchange of data presented in the form of tables. For example, Microsoft Excel allows you to export or import data using this format. The node-csv module simplifies the process of working with CSV data in JavaScript and provides tools for creating, processing, transforming CSV files, and allows you to convert them to strings. Its API supports callback functions, threads, there is also its synchronous version, which gives the developer the opportunity to choose exactly what he needs.

▍ 10. Markdown File Processing


Markdown is a popular format for creating web-based content. If you need to process markdown data programmatically (that is, for example, you want to create your own markdown editor), take a look at the marked module . At the input, it receives code in markdown format, and already outputs HTML code. At the same time, the tools of this module allow further processing of the received HTML code using rendering tools that are customizable by the developer.

Data Optimization and Minification


▍11. Image optimization and minification



Imagemin - a module for minification and optimization of images

Imagemin - a great module for minification and optimization of images. It can be used from the command line, in the form of a plugin for gulp or Grunt, you can also work with it using imagemin-app, a GUI application available for all the most common operating systems. The imagemin architecture is based on plugins. This indicates the flexibility of this module, and that its ability to support various graphic formats can be expanded.

▍ 12. HTML minification



The html-minifier tool can be considered the best of the existing HTML-minifier.

After optimizing the images, you should think about minifying the HTML code of the web application. This task can be solved by the html-minifier module , which can work from the command line, and is also available for gulp and Grunt. In addition, there are solutions that allow you to integrate it into web frameworks like Koa and Express, as a result of which HTML can be minified right during the server’s operation, before sending HTML pages to clients. In accordance with the benchmark results that are available on the module’s home page, this is the best existing tool for minifying HTML code.

▍ 13. CSS minification


Having minified and optimized the HTML code and images sent from the server to clients, it is worth considering the issue of CSS minification. This problem can be solved by the very fast clean-css module , which can be used both from the command line and from the JS code. It supports source maps, as well as various compatibility modes that allow you to work with the resulting minified CSS in older versions of IE.

▍14. JavaScript minification



The UglifyJS2 module can not only minify JavaScript code, but it solves this task very well. The

popular UglifyJS2 module is often used to minify JS code, but, thanks to its code parsing capabilities, it can, in principle, be used to solve a wide range of tasks, related to word processing programs in JavaScript. This module converts JavaScript code into an abstract syntax tree (this is an object model representing the code) and provides tools for traversing this tree. This module is also suitable for those who are considering writing their own JavaScript optimizer.

▍15. SVG minification


The topic of minifying SVG data is provided at the end of this section, but it is as important as everything that we talked about here. In the past few years, a resurgence of this format can be observed. The thing is that browsers perfectly support it, and that there are convenient tools for working with it. Unfortunately, the SVG data that editors generate often contains redundant or useless information like comments and metadata.

In order to remove all unnecessary data from SVG data, you can use the SVGO module . This module supports the plug-in system, here almost every optimization is presented as a separate plug-in. As with other modules designed for minification, SVGO can be used both from the command line and from JS code.

Utilities


▍ 16. Logging


When you work with complex web applications, both during development and production, a good logging library can be very useful in order to find errors that occur during the operation of these applications. The winston module is very popular in this area . It supports various ways of working with data, for example, it can output information to the console, write to a file, save it in a database (for example, in CouchDB, MongoDB or Redis), or even give access to them via HTTP for further processing.

▍17. Creating dummy data


During the development or testing of user interfaces, there is often a need for certain conditional data, such as email addresses, usernames, home addresses, phone numbers. The faker.js library can help in solving such problems . You can use it on the server (as a module for Node.js) and on the client. This tool provides a set of methods for creating dummy data. Do you need a username? Call the method faker.internet.userName()- and a randomly generated name is at your disposal. Want a company name? Refer to the method faker.company.companyName(). In fact, faker.js can help create almost any kind of data of this kind.

▍18. Sending emails



Nodemailer supports SSL / STARTTLS and can send messages containing plain text, HTML and images.

During the development of websites, it is often necessary to send emails programmatically. This is necessary, for example, for sending users registration confirmations, for sending out notifications about important events, news. In fact, there are many situations in which you need the ability to work with emails.
The standard Node.js API does not have facilities for sending emails. This problem can be solved using the Nodemailer module . It supports sending letters containing plain text, HTML, images, and, most importantly, supports a secure SSL / STARTTLS communication protocol.

▍19. Creating a REST API


REST is the de facto standard used to create web applications that use web services. Frames like Express help in creating such services, but often, “on the load”, the developer gets tools for working with templates and a rendering system, which, depending on the specific situation, may be unclaimed. In this situation, you should take a look at the restify module , which is focused exclusively on the development and testing of the REST-API. Its API is very similar to Connect middleware (which the Express framework is based on), but it provides the developer with a higher level of control over HTTP interactions and supports DTrace to find problems with applications in real time.

▍20. Creating command line applications


There are a huge number of command line applications created by Node.js tools and designed to solve a variety of tasks (for example, this applies to the tools for minifying and optimizing data discussed above). If you are considering creating your own command line application, pay attention to the Commander.js package . Its convenient tools allow you to describe various aspects of such applications, including commands, options, aliases, and reference materials. It greatly simplifies the process of creating command line applications.

Summary


In fact, in this article we examined only a small part of those useful tools that were created for Node.js. JavaScript is now incredibly popular, and new npm modules appear literally every week. If you want to find something interesting on your own, look at the npm homepage , paying attention to the projects that have received the highest community ratings, and look at GitHub , in the section of popular repositories.

Dear readers! If you are busy developing for the Node.js platform, you probably have a list of your favorite modules. We ask you to tell about them.


Also popular now: