Node.js — Debugging
console Methods
console.log('Basic log');
console.info('Info');
console.warn('Warning');
console.error('Error');
console.table([{ name: 'John' }, { name: 'Jane' }]);
console.time('timer');
// ... code ...
console.timeEnd('timer');
Node Inspector
node --inspect app.js
Then open chrome://inspect in Chrome.
Debugger Statement
function add(a, b) {
debugger; // Execution pauses here
return a + b;
}
VS Code Debugging
// .vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/app.js"
}
]
}
Debug Module
npm install debug
const debug = require('debug')('app:server');
debug('Server starting on port %d', 3000);
DEBUG=app:* node app.js
Profiling
node --prof app.js
node --prof-process isolate-*.log > processed.txt
Mini Practice
- Use console methods
- Debug with --inspect
- Set up VS Code debugging
- Use the debug module
Up Next
Continue with Async Programming — async patterns.
Related Topics
Frequently Asked Questions about Debugging
What is Debugging in Node.js?
Debugging is a fundamental concept in Node.js. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Debugging?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Debugging.
Why is Debugging important in Node.js?
Debugging is essential for Node.js development. Understanding this concept will help you write better code and solve real-world problems more effectively.