20 Node.js modules you need to know
We present our pick of the Node.js modules that will help you during the development of web applications and more.
Minification
11. Minify 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
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
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.
Get top Black Friday deals sent straight to your inbox: Sign up now!
We curate the best offers on creative kit and give our expert recommendations to save you time this Black Friday. Upgrade your setup for less with Creative Bloq.
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
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:
Thank you for reading 5 articles this month* Join now for unlimited access
Enjoy your first month for just £1 / $1 / €1
*Read 5 free articles per month without a subscription
Join now for unlimited access
Try first month for just £1 / $1 / €1