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.

If you use a Linux system that supports snaps for your development work, such as the Ubuntu desktop, you can install the Node.js snaps that are maintained by Nodesource. Otherwise, use the Nodesource package repositories.

A standard Node.js installation provides you with:

  • Node.js itself
  • An interactive shell (use the menu icon in Windows, or type node in a terminal window)
  • The npm package manager to install extra software

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 support for 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
  • 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 a basic package of features that you need for a Website or service, 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.

Cloud services such as Heroku, Google App Engine 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 framework.

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.

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.

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 Commander or the Open CLI Framework.

Packaging Applications

The tools for mobile and desktop application development build installable packages in the appropriate formats. To package other types of application, such as command-line tools, use pkg. This creates stand-alone executables that include Node.js itself, your code, and any other dependencies.

Resources

Interactive Tutorials

  • NodeSchool - Free installable tutorials for Node.js

Documents

Books