menu

Search By Label

you can use the debugger statement directly in your Node code to create breakpoints. When the Node runtime hits the debugger statement, it will pause execution if a debugger is attached.
function exampleFunction() {
    const value = 42;
    debugger; // Execution will pause here if a debugger is attached
    console.log(value);
}

exampleFunction();
Start Your Application in Debug Mode using the --inspect or --inspect-brk flag when running your app.
node --inspect --5120 lib/app.js
If all goes as expected you will see the port to access to the breakpoints, example:
Debugger listening on ws://127.0.0.1:9229/65b96f6d-6202-49db-bbe8-63b706a580a2
Visit the port on your browser and open the Node DevTools.
image.png 54.1 KB
More info: https://nodejs.org/en/learn/getting-started/debugging
To debug a Node.js application, one can use the debugging built-in method:

(1) Insert debugger; statement where you want to insert a break point
(2) Run the file with command $ node inspect <file name>
(3) Use a key for example, c to continue to next break point

You can even debug values associated to variables at that break point by typing repl. For more information, Please check the official guide.
CommonJS (Node.js):
  • Syntax: Utilizes require to import modules and module.exports or exports to define exports.
  • Loading: Synchronous, modules are loaded and executed sequentially.
  • Use Cases: Well-suited for server-side applications, especially in Node.js environments where synchronous loading is acceptable.

ES6 Modules:
  • Syntax: Uses import and export statements for importing and exporting modules.
  • Loading: Supports asynchronous loading, enabling more efficient loading of modules in browsers.
  • Use Cases: Widely used in front-end development due to support in modern browsers. Offers a more standardized syntax and supports advanced features like dynamic
  • imports.
process.nextTick is a Node.js feature that defers the execution of a callback function to the next iteration of the event loop. It's often used when immediate execution is necessary, but the function should be deferred to avoid blocking the event loop. For example, in scenarios where you want to ensure that certain asynchronous operations are complete before continuing, process.nextTick can be employed.
To prevent SQL injection in Node.js, use parameterized queries with a database library, such as pg for PostgreSQL or mysql2 for MySQL.

Example using pg for PostgreSQL:

const { Client } = require('pg');

// Bad: Concatenation (vulnerable to SQL injection)
const insecureQuery = `SELECT * FROM users WHERE username = '${inputUsername}'`;

// Good: Parameterized query (prevents SQL injection)
const secureQuery = 'SELECT * FROM users WHERE username = $1';
const values = [inputUsername];

const client = new Client();
client.connect();

client.query(secureQuery, values, (err, result) => {
  // Handle the query result
  client.end();
});

By using parameterized queries, you avoid directly interpolating user input into SQL statements, reducing the risk of SQL injection.

This command is useful for ensuring that your local npm cache is in a healthy state, which can help prevent issues when installing or updating packages. If any issues or corruption are detected, npm cache verify will attempt to fix them. It's a maintenance command to help maintain the reliability of your local npm package cache.