Jump to content
Slate Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate Marble
Slate Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate Marble
Sign in to follow this  
Rss Bot

20 Node.js modules you need to know

Recommended Posts

Over recent years Node.js has become more and more popular. It is now often used for developing the server side of web applications, or in general during the development process. At the time of writing, the homepage of npm – the package manager for Node.js – lists over a quarter of a million modules. 

I've put together a list of the ones I find useful in my daily work as a web and software developer, from image manipulation, string validation and PDF generation to minification, logging and the creation of command line applications.

Working with images

01. Manipulate images

GraphicsMagick and ImageMagick are two popular tools for creating, editing, composing and converting images. Thanks to the Node.js module gm you can use both tools directly from within your JavaScript code. The module supports all the typical image operations – resizing, clipping and encoding to name just a few.

02. Process images

Sharp is based on the ultra-fast libvips image processing library, and claims to be four to five times faster than ImageMagick or GraphicsMagick when it comes to compressing and resizing images. It supports JPEG, PNG, WebP, TIFF, GIF and SVG images, and outputs data into either JPEG, PNG, WebP or uncompressed raw pixel streams.

03. Generate sprite sheets

Sprite sheets are bitmap files that contain many different small images (for example icons), and they are often used to reduce the overhead of downloading images and speed up overall page load. Generating sprite sheets manually is very cumbersome, but with spritesmith you can automate the process. This module takes a folder as input and combines all the images in it into one sprite sheet. It also generates a JSON file that contains all the coordinates for each of the images in the resulting image, which you can directly copy in your CSS code.

Dates, strings, colours

04. Format dates

kKpBfi7DAHmtRQjNW2KF8n.jpg

Moment.js is a great alternative to JavaScript's Date object

The standard JavaScript API already comes with the Date object for working with dates and times. However, this object is not very user-friendly when it comes to printing and formatting dates. On the other hand, Moment.js offers a clean and fluid API, and the resulting code is very readable and easy to understand. 

In addition, there is an add-on available for parsing and formatting dates in different time zones.

05. Validate strings

When providing forms on a web page, you always should validate the values the user inputs – not only on the client-side, but also on the server-side to prevent malicious data. A module that can help you here is validator.js. It provides several methods for validating strings, from isEmail() and isURL() to isMobilePhone() or isCreditCard(), plus you can use it on the server- and the client-side.

06. Work with colour values

Converting colour values from one format into another is one of the tasks every frontend developer needs to do once in a while. TinyColor2 takes care of this programmatically, and it's available for Node.js as well as for browsers. It provides a set of conversion methods (e.g. toHexString(), toRGBString()), as well as methods for all sorts of colour operations (e.g. lighten(), saturate(), complement()).

Working with different formats

07. Generate PDF files

You want to dynamically generate PDF files? Then PDFKit is the module you are looking for. It supports embedding font types, embedding images and the definition of vector graphics, either programmatically (using a Canvas-like API) or by specifying SVG paths. Furthermore, you can define links, include notes, highlight text and more. The best way to start is the interactive browser demo, which is available here.

08. Process HTML files

dBRbmiP3PPJNk9pJU6BmBn.jpg

Cheerio makes processing HTML on the server side much easier

Ever wanted to process HTML code on the server side and missed the jQuery utility methods? ThenCheerio is the answer. Although it implements only a subset of the core jQuery library, it makes processing HTML on the server side much easier. It is built on top of the htmlparser2 module, an HTML, XML and RSS parser. Plus, according to benchmarks, it's eight times faster than jsdom, another module for working with the DOM on the server side.

09. Process CSV files

PjbCzT7w8qpVG5sDWajD8n.jpg

Node-cvg simplifies the process of working with CSV data

The CSV (comma-separated values) format is often used when interchanging table-based data. For example, Microsoft Excel allows you to export or import your data in that format. node-cvg simplifies the process of working with CSV data in JavaScript, and provides functionalities for generating, parsing, transforming and stringifying CSV. It comes with a callback API, a stream API and a synchronous API, so you can choose the style you prefer.

10. Process markdown files

Markdown is a popular format when creating content for the web. If you ever wanted to process markdown content programmatically (i.e. write your own markdown editor), marked is worth a look. It takes a string of markdown code as input and outputs the appropriate HTML code. It is even possible to further customise that HTML output by providing custom renderers.

Next page: Explore the best minifiers and utility modules

Minification

11. Minify images

F4QEyr78F3bx9oJwNvsG8n.jpg

Imagemin is a brilliant module for minifying and optimising images

A very good module for minifying and optimising images is imagemin, which can be used programmatically (via the command line), as a gulp or Grunt plugin, or through imagemin-app (a graphical application available for all of the three big OSs). Its plugin-based architecture means it is also very flexible, and can be extended to support new image formats.

12. Minify HTML

sipfK9yQfA92JyxL3wBP8n.jpg

This claims to be the best HTML minifier available

After minifying images you should consider minifying your web app's HTML. The module HTMLMinifier can be used via the command line, but is also available for gulp and Grunt. On top of that, there are middleware solutions for integrating it into web frameworks like Koa and Express, so you can minify the HTML directly at runtime before serving it to the client via HTTP. According to benchmarks on the module's homepage, it is the best HTML minifier available.

13. Minify CSS

As well as images and HTML, you should consider minifying the CSS you send the user. A very fast module in this regard is clean-css, which can be used both from the command line and programmatically. It comes with support for source maps and also provides different compatibility modes to ensure the minified CSS is compatible with older versions of IE.

14. Minify JavaScript

rvbCMFrexqCVFQ7akiX2Cn.jpg

UglifyJS2 isn't just for minifying code, but it's very good at it

The popular module UglifyJS2 is often used for minifying JavaScript code, but because of its parsing features, in principle you can use it to do anything related to processing JavaScript code. UglifyJS2 parses JavaScript code into an abstract syntax tree (an object model that represents the code) and provides a tree walker component that can be used to traverse that tree. Ever wanted to write your own JavaScript optimiser? Then UglifyJS2 is for you.

15. Minify SVG

Last but not least when it comes to minification, don't forget to minify the SVG content. This format has made a great comeback in the past few years, thanks to its great browser and tool support. Unfortunately, the SVG content that is generated by editors often contains redundant and useless information like comments and metadata. 

With SVGO you can easily remove such information and create a minified version of your SVG content. The module has a plugin-based architecture, with (almost) every optimisation implemented as a separate plugin. As with all the other modules regarding minification, SVGO can be used either via the command line or programmatically.

Utilities

16. Log application output

When you are dealing with complex web applications a proper logging library can be very useful to help you find runtime problems, both during development and in production. A very popular module in this regard is the winston library. It supports multiple transports, meaning you can tell winston to simply log to the console, but also to store logs in files or in databases (like CouchDB, MongoDB or Redis) or even stream them to an HTTP endpoint for further processing.

17. Generate fake data

When implementing or testing user interfaces you often need dummy data such as email addresses, user names, street addresses and phone numbers. That is where faker.js comes into play. This can be used either on the server side (as a module for Node.js) or on the client side, and provides a set of methods for generating fake data. Need a user name? Just call faker.internet.userName() and you get a random one. Need a fake company name? Call faker.company.companyName() and you get one. And there are a lot more methods for all types of data.

18. Send emails

Xz4crhrZkRycZ3iddchS8n.jpg

Nodemailer supports text and HTML content, embedded images and SSL/STARTTLS

Programmatically sending emails is one of the features you need often when implementing websites. From registration confirmation, to notifying users of special events or sending newsletters, there are a lot of use cases that require you to get in touch with users. 

The standard Node.js API does not offer such a feature, but fortunately the module Nodemailer fills this gap. It supports both text and HTML content, embedded images and – most importantly – it uses the secure SSL/STARTTLS protocol.

19. Create REST APIs

REST is the de facto standard when implementing web applications that make use of web services. Frameworks like Express facilitate the creation of such web services, but often come with a lot of features such as templating and rendering that – depending on the use case – you may not need. On the other hand, the Node.js module restify focuses on the creation and the debugging of REST APIs. It has a very similar API to the Connect middleware (which is the base for Express) but gives you more control over HTTP interactions and also supports DTrace for troubleshooting applications in real time. 

20. Create CLI applications

There are already tons of command line applications (CLI applications) written in Node.js to address different use cases (see, for example, the aforementioned modules for minification). If you want to write your own CLI application, the module Commander.js is a very good starting point. It provides a fluent API for defining various aspects of CLI applications like the commands, options, aliases, help and many more, and it really simplifies the process of creating applications for the command line.

Conclusion

We've only scratched the surface of the huge number of Node.js modules out there. JavaScript is more popular than ever before and there are new modules popping up every week. A good place to stay up to date is the 'most starred packages' section of the npm homepage or Github's list of trending repositories

Related articles:

View the full article

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×