Top 5 Node.js Tools for Debugging and Profiling

Sedang Trending 2 bulan yang lalu

Explore nan Built successful Debugger, WebStorm, Built In Profiler, Node Inspector and nan VSCode Debugger

Binara Prabhanga

Bits and Pieces

Node.js requires due devices for debugging and profiling.

Debugging and profiling are basal processes for uncovering and fixing errors, measuring and optimizing performance, and ensuring nan value and ratio of your Node.js applications.

But location are galore devices disposable for debugging and profiling Node.js applications, truthful really do you take nan champion ones for your needs?

Well, that’s wherever this article comes in. Let’s look astatine nan apical 5 Node.js devices for debugging and profiling.

Node.js Built-in Debugger is simply a elemental and effective instrumentality that comes pinch Node.js itself. You tin usage it to inspect your code, group breakpoints, and watch variables. To usage it, you conscionable request to tally your Node.js exertion pinch nan --inspect emblem and link to it pinch Chrome DevTools aliases nan Node debugging client.

Pricing: The Node.js Built-in Debugger is free to use, arsenic it is portion of nan Node.js platform. You tin download Node.js from its charismatic website.

To group up and usage nan Node.js Built-in Debugger, you tin travel these steps:

Step 01

Create a Node.js app aliases usage an existing one. For this example, I will usage a elemental app that prints “Hello, world!” and past throws an error. Save it arsenic app.js successful your task folder.

// app.js
const definitive = require('express');
const app = express();

app.get('/', (req, res) => {
res.send('Hello, world!');
propulsion caller Error('Oops!');
});

app.listen(3000, () => {
console.log('Server listening connected larboard 3000');
});

Step 02

Open a terminal and navigate to your task folder. Then, tally your app pinch nan beneath command.

node --inspect app.js

This will commencement your app and alteration nan debugger. You should spot a connection for illustration this successful nan terminal:

Debugger listening connected ws://127.0.0.1:9229/uuid-here
For help, see: https://nodejs.org/en/docs/inspector
Hello, world!

Step 03

Open different terminal and tally nan bid node inspect 127.0.0.1:9229/uuid-here, replacing nan uuid-here portion pinch nan existent UUID from nan erstwhile message. This will link to nan debugger and region nan execution of your app. You should spot a punctual for illustration this:

< Debugger listening connected ws://127.0.0.1:9229/uuid-here
< For help, see: https://nodejs.org/en/docs/inspector
connecting to 127.0.0.1:9229/uuid-here ... ok
break successful app.js:1
> 1 console.log('Hello, world!');
2
3 propulsion caller Error('Oops!');
debug>

Step 04

At nan debug prompt, you tin usage various commands to power nan execution of your app and inspect its state. You tin usage commands for illustration n, c, s, o, repl, watch, list, setBreakpoint, etc. to power and inspect your app. Let’s usage nan n bid to measurement to nan adjacent line. You should spot nan output of nan console.log connection and nan existent statement updated to nan propulsion statement.

debug> n
Hello, world!
break successful app.js:3
1 console.log('Hello, world!');
2
> 3 propulsion caller Error('Oops!');
4
debug>

Step 05

Next, let’s usage nan c bid to proceed nan execution until nan adjacent breakpoint aliases nan extremity of nan app.

debug> c
break successful internal/errors.js:259
257 }
258 }
>259 return ${prefix}${this.message} ;
260 };
261
Watchers:
0: Error = usability Error() { [native code] }

Step 06

Finally, usage nan discontinue bid to exit nan debugger.

debug> quit
< undefined

These are nan basal steps to debug your Node.js app utilizing nan Node.js Built-in Debugger.

WebStorm is simply a powerful and versatile IDE for Node.js development, pinch a built-in debugger that supports Node.js halfway and celebrated frameworks. You tin usage it to debug your Node.js exertion pinch a graphical interface, edit codification connected nan fly, and usage various features for illustration codification completion, refactoring, testing, and more. You tin download WebStorm from its charismatic website.

Pricing: WebStorm has different pricing and licensing options. The existent prices are arsenic follows:

  • For individuals: $69 for nan first year, $55 for nan 2nd year, and $41 for nan 3rd twelvemonth onwards.
  • For organizations: $159 per personification for nan first year, $127 for nan 2nd year, and $95 for nan 3rd twelvemonth onwards.
  • For students and teachers: Free.
  • For open-source projects: Free for qualified projects.

To group up and usage WebStorm for debugging your Node.js app, you tin travel these steps:

Step 01

Create aliases unfastened a task pinch your app file. For this example, I will usage a elemental app that prints “Hello, world!” and past throws an error.

Step 02

Go to **Run | Edit Configurations** from nan paper barroom aliases click nan **Edit Configurations** fastener connected nan toolbar. In nan dialog that opens, click nan **Add** fastener and prime **Node.js** from nan list. This will create a caller Node.js run/debug configuration for your app.

Step 03

In nan **Node.js** configuration dialog, group nan name, interpreter, file, parameters, and environmentvariables for your app. Click OKto prevention nan configuration and spot it connected nan toolbar.

Step 04

Start debugging your app and usage nan debug toolbar and tabs to power and inspect your app. You tin besides measure expressions successful nan editor aliases nan **Debug Console** tab by hovering complete them aliases typing them.

Node.js Built-in Profiler is simply a command-line instrumentality included pinch Node.js that helps developers place capacity issues successful their applications. It generates output successful nan shape of a V8 log record that tin beryllium analyzed to summation insights into nan functions called during nan profiling convention and their execution times

Pricing: Node.js Built-in Profiler is free to use, arsenic it is portion of nan Node.js program.

To group up and usage Node.js Built-in Profiler for profiling your Node.js app, you tin travel these steps.

Step 01

Run your app pinch nan --prof emblem and nan NODE_ENV=production variable. For example,

NODE_ENV=production node --prof app.js

This will commencement your app and make a log record successful nan aforesaid folder, pinch a sanction for illustration isolate-0x102801c00-v8.log.

Step 02

Put immoderate load connected your app by sending requests to it. You tin usage a instrumentality for illustration curl aliases ab (ApacheBench) to do this. For example,

curl -X GET "http://localhost:3000/"
ab -k -c 20 -n 250 "http://localhost:3000/"

This will nonstop a GET petition to your app utilizing curl, and past nonstop 250 concurrent requests utilizing ab.

Step 03

Stop your app pinch Ctrl+C and process nan log record pinch nan --prof-process flag. For example,

node --prof-process isolate-0x102801c00-v8.log > profile.txt

This will create a profile.txt record successful nan aforesaid folder, pinch nan processed data. This will show you thing for illustration this:

Statistical profiling consequence from isolate-0x102801c00-v8.log, (250 ticks, 0 unaccounted, 0 excluded).

[Shared libraries]:
ticks full nonlib name
9 3.6% /usr/lib/...
2 0.8% /usr/lib/...

Node Inspector is simply a standalone debugger that provides a akin acquisition to Chrome DevTools. You tin usage it to debug and floor plan your Node.js exertion successful a abstracted browser window, pinch features for illustration real-time codification editing, capacity profiling, and more.

Pricing: Node Inspector is free to use, arsenic it is an open-source and cross-platform software. You tin instal it utilizing npm.

To group up and usage Node Inspector for profiling your Node.js app, you tin travel these steps:

Step 01

Install it globally pinch npm and commencement it pinch node-inspector command.

npm instal -g node-inspector
node-inspector

This will motorboat Node Inspector and people a URL to entree it successful nan browser. By default, it will beryllium thing for illustration http://127.0.0.1:8080/?port=5858.

Step 02

Run your app pinch nan --inspect emblem and nan aforesaid larboard number arsenic Node Inspector.

node --inspect=5858 app.js

Open nan Node Inspector URL successful your browser and usage nan Profiles tab to grounds a CPU floor plan of your app.

Step 03

To floor plan your Node.js app, spell to nan Profiles tab and click nan Start button. This will commencement signaling a CPU floor plan of your app. Put immoderate load connected your app by sending requests to it. You tin usage a instrumentality for illustration curl aliases ab (ApacheBench) to do this.

curl -X GET "http://localhost:3000/"
ab -k -c 20 -n 250 "http://localhost:3000/"

Step 04

Stop nan profiling by clicking nan extremity fastener and position nan floor plan study to place capacity bottlenecks and hotspots successful your code.

Visual Studio Code Debugger is simply a feature-rich and user-friendly debugger that integrates pinch Visual Studio Code, a celebrated codification editor. You tin usage it to debug your Node.js exertion pinch a graphical interface, edit codification connected nan fly, and usage various extensions.

Pricing: Visual Studio Code Debugger is free to use, arsenic it is portion of nan Visual Studio Code editor, which is an open-source and cross-platform software.

To group up and usage Visual Studio Code Debugger for debugging and profiling your Node.js app, you tin travel these steps:

Step 01

Open Visual Studio Code and create a caller task aliases unfastened an existing one. For this example, I will usage a elemental Express app that prints “Hello, world!” and past throws an error. Save it successful your task folder.

Go to nan **Run and Debug** position by clicking nan **Run and Debug** icon successful nan Activity Bar connected nan near broadside of Visual Studio Code. If moving and debugging are not yet configured (no launch.json has been created), Visual Studio Code shows nan Run commencement view.

Step 02

Click nan **create a launch.json file** nexus successful nan Run Start view. Select **Node.js** arsenic nan environment. This will make a launch.json record successful nan .vscode files of your project, pinch immoderate default settings. You tin edit this record according to your needs. Here is an illustration of a launch.json file

{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": ["<node_internals>/**"],
"program": "${workspaceFolder}/app.js",
// Enable profiling for this configuration
"profile": true
}
]
}

Step 03

Set a breakpoint successful your codification wherever you want to region nan execution. Then, commencement debugging your app and usage nan debug toolbar and tabs to power and inspect your app.

Step 04

End nan debugging convention and unfastened nan CPU floor plan record to spot nan capacity of your app. For much information, spot Debugging successful Visual Studio Code.

Step 05

Visual Studio Code Debugger creates a CPU floor plan record successful .vscode files aft debugging. The record shows a occurrence chart of nan CPU usage of your app, pinch nan functions and their times. You tin usage this accusation to place capacity bottlenecks and hotspots successful your code. For much information, spot Profiling Node.js apps here.

Debugging and profiling are basal for processing high-quality and high-performance Node.js applications.

Depending connected your needs and preferences, you whitethorn find different devices much suitable and effective for your projects. Some of nan factors you should see erstwhile selecting a instrumentality are its features, usability, and popularity.

By comparing and evaluating these factors, you tin find nan instrumentality that meets your expectations and helps you troubleshoot and optimize your Node.js applications.