Starting Node.js Development

Posted in programming with tags node.js -

Summary

Notes on development with Node.js and JavaScript.

Installing Node.js

To install Node.js on Windows, download it from the official Website. Choose the 64-bit Windows installer package for the current LTS version, unless you know that you need a different option.

To install Node.js on macOS, use Homebrew.

To install Node.js on Linux, use the recommended installation process for your distribution, or a Docker image. If the distribution supports snaps, you can also install Node.js as a snap. Snaps automatically update, so are not suitable for situations where you need a reproducible environment. Nodesource maintain the Linux packages for popular distributions, and Docker, Inc. maintain the Docker images.

Choosing a Code Editor or IDE

My preferred text editor is currently Visual Studio Code. This tutorial shows you the features that Visual Studio Code has for JavaScript development, including code quality checks, and support for debugging. You may prefer Atom, which is a high-quality and customisable editor that has plugins for Node.js and JavaScript, or Notepad++, which will be more suitable for older computers with limited resources. All of these editors are free.

Essential Tools

There are a number of de-facto standard utilities and libraries for JavaScript software development, but a few tools are so fundamental that you should install them even before you begin to write JavaScript code.

Git for Version Control

If you do not already use version control, you should install Git on your system. Git is now effectively the standard version control tool for developers.

Version control is obviously vital for collaborating with other programmers. It also enables you to efficiently copy your application to other systems for testing, deployment and backup.

If Git is installed, Atom and Visual Studio Code provide you with access to information and features from Git directly in their user interfaces. If you use Visual Studio Code, you should also consider installing the Git Lens extension, which enhances the integration with Git.

Code Quality

Set up ESLint in all of your projects to run code quality checks. Add Prettier, which will format your code, removing style issues. Prettier integrates with ESLint, so that ESLint formats your code with Prettier, and then checks the reformatted code.

Plugins enable the popular text editors and IDEs to integrate ESLint and Prettier, so that your code can be formatted and checked as you work.

Testing Tools

Jest provides a comprehensive set of tools for testing. If you do not already have a preference, add Jest to your project.

Existing JavaScript projects may use a combination of libraries in their test suite. The most popular solution is to use the Mocha unit testing framework, in conjunction with Sinon.js for mocks, Chai for assertions, Karma for running tests, and Istanbul to measure test coverage.

Other Development Tools

You should probably learn these as you need them.

  • Babel - Compiles JavaScript code into alternate versions
  • Documentation.js - Documentation generator that uses the standard JSDoc format
  • Nodemon - Instant code reloading during development
  • PM2 - Process manager for Node.js applications
  • Webpack - Web assets compiler

Additional Libraries

By design, Node.js only includes a very minimal library of modules. These packages offer commonly used items:

Web Applications

Express offers the basic elements that you need for a Website or service, along with support for a large range of plugins, and is the most popular choice. Feathers builds on Express to provide support for REST APIs, real-time messaging, database access, and other capabilities. The hapi framework is designed for larger applications. Fastify is an emerging alternative to Express and hapi that can use Express plugins.

Cloud services such as Google App Engine, Heroku, Red Hat OpenShift and Zeit Now provide low-maintenance hosting for Node.js Web applications.

To produce applications for function as a service infrastructure, such as AWS Lambda, use the Serverless or Claudia.js frameworks.

Web Clients

Use the axios library for your Web client software, such as downloading files or working with APIs. The HTTP software that is included with Node.js uses callbacks, rather than the newer promises style of API.

Cheerio provides an implementation of the jQuery API for reading and processing HTML documents with Node.js.

Puppeteer enables you to automate copies of Google Chrome and Chromium Web browsers.

Accessing Databases

Knex provides a toolkit for working with SQL databases, including query building, connection handling, and schema migrations. Objection.js is an Object Relational Mapper (ORM) that builds on Knex. TypeORM is an emerging alternative to Knex and Objection.js, with a broader range of features. To work with MongoDB, use Mongoose.

Driver software required: To access a database service such as PostgreSQL, Redis, or MongoDB, you will need to install the appropriate Node.js driver.

Graphical Desktop Applications

To create desktop applications, use Electron. Applications made with Electron are cross-platform, and can be built for Windows, macOS, and Linux with electron-builder.

Mobile Applications

To develop mobile applications with JavaScript, use either Ionic or React Native. Ionic creates hybrid mobile apps that use HTML, CSS and JavaScript with the Angular framework and Apache Cordova. React Native translates JavaScript into instructions to the native APIs of each mobile operating system.

Developing Command-line Tools

To create command-line tools with Node.js, use either Commander, or the Open CLI Framework. Commander is the most popular choice for building command-line utilities. The Open CLI Framework was developed by Heroku for their command-ine tools, and then released as an Open Source project in 2018.

To package command-line tools, use pkg. This creates stand-alone executables that include Node.js itself, your code, and any other dependencies.

Robotics and Internet of Things

Johnny-Five is the main package for working with robotics and hardware, such as Arduino boards.

The Espruino is a single-chip board with a microcontroller that is specifically designed to be programmed with JavaScript.

Resources

Interactive

  • freeCodeCamp - Free courses for new programmers
  • NodeSchool - Free installable tutorials for Node.js
  • Nodebots - The community for JavaScript robotics and Internet of Things

Documents

Books