Debugging node js apps: Ultimate 9 tips and tricks for Node.js application

debugging

Whatever an object might be, it needs maintenance. To simplify, error is unavoidable, right? Unless the mistake is quite obvious, what to do if your Node.js apps have unknown issues? In this case, debugging node js apps is a great help!

Today, ArrowHiTech‘s committed team would love to help you out! We gladly present our table of contents:

  1. Definition
  2. Importance
  3. Top 9 tips to debug
  4. Final Words

Without further ado, let’s get started!

What is debugging node js apps?

Debugging is the process of finding and fixing bugs. In other words, it covers such a difficult and essential part when you have to rectify any problems. 

For ones with basic knowledge and starters, remember these terms:

  • debugger: tool used for debugging
  • log point: instruction that shows a value of variables to debuggers in time of execution
  • logging: outcome of runtime info to a file
  • refactor: to rewrite code for better maintenance
  • RTFM: ‘Read the flipping manual’
  • severity: impact of a bug on a system
  • syntax error: typo errors
  • watchpoint: when a program is stopped if a variable have its value

Top 9 tips and tricks for debugging node js apps

1. Source Control

A typical contender is GitHub and Bitbucket. To clarify, Source control helps manage vision and protect the codes. Thus, when a bug is discovered, you will find it easy to know where to start debugging.

2. Issue-tracking System – debugging node js apps

By adopting a tracking system, it is quick and simple to report bugs. Moreover, it helps document steps, determine severity and even track progress in the debugging stages. In this case, we truly recommend going for an app development company!

Remote Application Developer: Key strategy to find one that tailor your project and budget

3. Code Editor

Not just a code editor, a high quality code editor. Some amazing features that better your app performance and visual are:

  • numbering
  • color coding
  • formatting
  • auto-indentation
  • snippet
  • navigation
  • refactoring
  • code detection
  • suggestions
  • and many more…

In Node.js, the most popular free editors are VS Code, Brackets and Atom. However, based on your budget and target, you can choose from plenty of them.

debugging vs code

4. Code Linter – debugging node js apps

Code Linter is used to report faults such as syntax errors or bad indents. This is a great plugin to add thanks to its convenience. Some common ones that we highly suggest are: EsLint (for VS Code) and linter-eslint (for Atom).

5. TDD – Test driven Development

This is a process that helps write codes to test operation. Usually, the codes are tested to see and prove a specific function and spot any issues. In short, it simplifies the debugging node js apps process.

debugging tdd
TDD cycle

6. Alternative console – debugging node js apps

There are actually many types of consoles besides console log that everyone knows. Let us show you each one with its strength:

console.groupconsole.tableconsole.dir
ExampleI am a console log indented by a single level
I am a multilevel nested console log
Strengthcreate collection of logs and nest all logs together in browsersuseful for a large objects with lots of features and for better structurespecify the depth of console log

Be flexible with each type of console to optimize your different systems!

7. Debugging with Chrome

Here, we use Node.js inspector:

node --inspect ./index.js

With local debugging, clients would be attach to:

Debugger listening on ws://127.0.0.1:9229/20ac75ae-90c5-4db6-af6b-d9d74592572f

As an alternative, simply use --inspect-brk to set breakpoint. Next, in Chrome, open chrome://inspect in the address bar. Check out these areas to adjust to your app:

  • + Add folder to workspace
  • Watch pane
  • Call Stack
  • Scope
  • Breakpoints
  • above Debugger paused: resume, step over/into/out/through and pause
debugging chrome
Debugging with Chrome

8. Debugging with VS Code

IDE or Integrated Developer Environments let you do debugging node js apps remotely. And because VS Code is the most popular JS IDE, here is the guide to help you set up debugging with VS Code:

Firstly, copy and save this file as app.js

const express = require('express');
const app = express();

app.get('/', (req, res) => {
    res.send('Hello World!');
});

app.listen(3000, () => console.info('Example app listening on port 3000!'));

Next, run the app with this command:

node app.js

There you go, your node process is accessible to debugging node js apps. Thus, the console should show:

Debugger listening on ws://127.0.0.1:9229/d5efc834-41ae-4758-a9e6-4d8767c5752a

After choosing debug option, it will show like this:

Debugger attached.

9. Advanced Debugging Configuration – debugging node js apps

To edit, click the icon at the top right. Then, choose Add Configuration. Here, VS Code can launch a process (via Node.js) or attach to Node.js inspection.

That’s all for the top 9 tips you need to know. Still having difficulty?

Looking for help? Contact ArrowHiTech NOW!

If you still have trouble debugging node js apps, why not hiring experts? It is a very wise investment, which leads to time saving, cost effectiveness and outstanding results. ArrowHiTech is proud to be one of the leading IT Outsourcing agencies in Vietnam.

With over 12 years in the industries, satisfying more than 10,000 clients globally, we are confident that your issues will be solved in no time! Our services and solutions are fast, low cost and always bring a smile to all clients’ face! You can check out our portfolio as well as our Node Development Services for more details!

Bottom Line

Today, we have delivered an article on useful ways for Debugging Node.js apps. ArrowHiTech hopes that you find it helpful for your journey.

And as always, we truly wish the best of luck and success will come to you! WE’D LOVE TO HEAR FROM YOU AT:

Tags

Share