6 common mistakes developers make in Node JS

As with every platform and programming language, Node JS has its fair share of critique and praise. Typically, the criticism revolves around safe code writing practises and how it is complicated in Node and so on. Having been into Node.js development for a long while now, we have come with some of the common Node JS mistakes that developers should avoid; read on to learn more.

1. Call backs being invoked multiple times

This is surprisingly a very common mistake developers make and when someone else makes use of the code, they might be in for a huge surprise! After all, a call back isn’t the end point of the current function and some developers have the practise of adding a return before the call back to avoid multiple invocations. In most functions that are asynchronous in nature, the return statement is practically insignificant and thus helps in avoiding this problem. We recommend that you keep this in mind while working on Node.js development.

2. Call backs don’t run synchronously but most developers expect them to!

In JavaScript, things don’t get executed in the same order and sometimes you may wish to fire a statement after the call back and assume that it would work so but in reality it might not work that way. We recommend that you actually add whatever needs to be done immediately after the call back inside it to ensure the order of execution.

3. Error throws from call backs

Looks like most of the trouble brews with call backs, huh? Again the problem here is with asynchronous behaviour. Your try and catch may not behave in the manner that you wish it to. This is why most Node.Js developers actually follow the (err,..) pattern for the arguments of the call back function wherein the error is the first argument of the call back.

4. Numbers are floating data types in JS

Assuming numbers to be integer data types could be potentially problematic because they actually are floating point data types in JavaScript. The moment the limits of the float are breached, trouble beings and calculations could go awry! Also the operators work differently with integers and floats! Remembering this could ensure that you don’t have a problem with the way your function is supposed to work and get the desired results.

5. Using Console.log to debug instead of the debug library

We can understand that it is indeed tempting to print out everything using console.log and later comment it out. We recommend that you make use of the Debug library for debugging as it meant for just that very purpose and also you can prevent logs from being printed by simply turning off the debug mode thus ensuring safety and preventing leaks.

6. Handling the event loop

Typically concurrency is achieved in Node by running Input Output Operations in an asynchronous fashion. If you wish to perform some operation that is CPU intensive with a front facing Node that clients use concurrently, you are in for trouble. Look for better solutions to handle this and avoid blocking the event loop.

Some of the other common mistakes that we find in usual Node.Js development are: not streaming APIs, not using Supervisor programs such as nodemon, supervisor and so on, assignment to exports instead of the module.exports and understanding the difference between them and finally call backs that are deeply nestled. Avoiding all of these could help you write better code; do try these out!

How can we help you?

Have an experienced software architect provide a free estimate & professional consultation.

Looking for a First-Class Software Consultant?