Categories
Node.js Best Practices

Node.js Best Practices — Modern Features

Spread the love

Like any kind of apps, JavaScript apps also have to be written well.

Otherwise, we run into all kinds of issues later on.

In this article, we’ll look at some best practices we should follow when writing Node apps.

Use ES2015+

E2015 and later features have been supported by Node since v4.

This means that we can use those features in very early versions.

There’s no excuse not to use them now.

We don’t need Babel to use the latest features.

Most of the stage 4 features are available.

If we aren’t sure if our version of Node supports the feature we want, we can check node.green to be sure.

Use Promises

Promises let us write async code in a clean way.

They make our lives a lot easier.

Instead of writng:

fs.readFile('./foo.json', 'utf-8', (err, data) => {
  if (err) {
    return console.log(err)
  }

try { JSON.parse(data) } catch (ex) { return console.log(ex) } console.log(data.name) })


We write:

import * as Promise from "bluebird"; const fs = Promise.promisifyAll(require("fs"));

fs.readFileAsync('./foo.json')
.then(JSON.parse)
.then((data) => {
  console.log(data.name)
})
.catch((e) => {
  console.error('error reading file', e)
})
```

It’s much cleaner since there’s less nesting with multiple promises.

We used Bluebird to promise the whole fs module so we can use the promise versions of the provided methods.

### Use the JavaScript Standard Style

To make everyone's lives easier, we can use standard styles for JavaScript code.

This way, we don’t have to fight about formatting.

The [JavaScript Standard Style](https://github.com/feross/standard) is a useful style guide that covers most JavaScript syntax with its own rules.

Now we don’t have to make decisions about `.eslintrc` , `.jshintrc` and other linting config files.

We just use the ones provided by them.

### Use Docker

Docker makes everything easy.

It lets us run our app in isolation.

They’re lightweight.

There’re no manual steps needed for deployments.

The deployments are immutable.

And we can easily mirror production environments locally with it.

### Monitor Our Applications

If our apps are used by users, we got to make sure that they’re up.

The only way that we know is to monitor them.

We can do that with tools like [Prometheus](https://prometheus.io/).

It’ll alert us of any issues.

Also, it’ll show us the CPU and memory usage of our app.

Distributed tracking and error searching can also be done.

Performance monitoring is also built-in.

We can also use it to check for security vulnerabilities in the NPM packages we use.

### Use Messaging for Background Processes

If we have background processes, we should send messages between them so that we can retain when one end is down.

We can use some message queuing solutions for this. Examples include:

*   [RabbitMQ](https://www.rabbitmq.com/)
*   [Kafka](https://kafka.apache.org/)
*   [NSQ](http://nsq.io/)
*   [AWS SQS](https://aws.amazon.com/sqs/)

### Use the Latest LTS Node Version

The LTS Node version is supported longer than the non-LTS versions.

Support includes security patches and other bug fixes.

Therefore, we would want to use them.

To switch easily, we can use `nvm` .

Once we install ti, we can run:

```
nvm install 12.16.1
nvm use 12.16.1
```

to install and use version 12.16.1 respectively.

We can also specify the Node version in the Dockerfile.

### Conclusion

We can make our lives easier with some modern technologies like using ES2015+ and Dockerizing our app.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *