Debugging a Node.js app using Chrome DevTools

When it comes to programming, it’s often necessary to test and experiment with code quickly. While it’s easy to debug client-side code using Chrome DevTools, debugging Node.js code with access to the filesystem and other Node.js capabilities might seem a bit challenging. However, it’s actually quite simple. To debug a Node.js app using Chrome DevTools, follow these steps: Open your terminal and run the following command: node --inspect This will start the Node....

Differences between Node and the Browser

Node.js and the browser both utilize JavaScript as their programming language. However, there are several key differences that set them apart. This article will explore these differences and highlight how writing JavaScript applications in Node.js differs from programming for the web inside the browser. As a frontend developer who extensively uses JavaScript, one of the significant advantages of Node.js is the ability to program everything, both the frontend and the backend, in a single language....

Error Handling in Node.js: Best Practices and Techniques for Robust Applications

Error handling is a crucial aspect of any Node.js application development. Failing to handle errors properly can lead to crashes, bugs, and security vulnerabilities. In this blog post, we will explore various techniques and best practices for effective error handling in Node.js. Creating Exceptions In Node.js, errors are handled through exceptions. To throw an exception, you can use the throw keyword followed by a value. In client-side code, the value can be any JavaScript value, such as a string, number, or object....

Exposing Functionality from a Node File Using `exports`

Learn how to use the module.exports API to expose data to other files in your Node.js application or even to other applications. Node.js comes with a built-in module system that allows files to import functionality from other files. To import something, you can use the following syntax: const library = require('./library'); This imports the functionality that is exposed in the library.js file located in the same folder as the current file....

Getting File Details in Node.js

Learn how to retrieve file details using Node.js Files contain various details that can be inspected using Node.js. One way to obtain these details is by using the stat() method provided by the fs module. To use this method, simply pass the file path as a parameter. Once Node.js retrieves the file details, it will invoke the callback function you provide with two parameters: an error message (if applicable) and the file stats....

How to Check the Current Node.js Version at Runtime

To check the current Node.js version at runtime, you have a few options. The first option is to use the version property of the process object. By running process.version, you will receive a string that represents the current Node.js version. Here is an example: console.log(process.version); However, please note that this approach will result in a ReferenceError in the browser, as the process object is not defined there. Alternatively, you can use process....

How to Exit from a Node.js Program: A Guide to Gracefully Terminate Your Application

In this technical blog post, we will explore different ways to terminate a Node.js application. While pressing ctrl-C in the console can close a program, we are more interested in discussing programmatically exiting. Let’s start with the most drastic method, which should be avoided. The process core module provides a method called process.exit() that allows you to programmatically exit from a Node.js program. When this line is executed, the process is immediately forced to terminate....

How to Fix an Issue Installing Node `canvas` on macOS

If you’ve encountered difficulties while trying to install the Node.js canvas library on macOS, I’m here to help you resolve the issue. When attempting to execute npm install canvas, you might have received error messages similar to the following: npm ERR! code 1 npm ERR! path /Users/flaviocopes/dev/old/generate-images-posts/node_modules/canvas npm ERR! command failed npm ERR! command sh -c node-pre-gyp install --fallback-to-build npm ERR! Failed to execute '/opt/homebrew/Cellar/[email protected]/bin/node /opt/homebrew/Cellar/[email protected]/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js configure --fallback-to-build --module=/Users/flaviocopes/dev/old/generate-images-posts/node_modules/canvas/build/Release/canvas.node --module_name=canvas --module_path=/Users/flaviocopes/dev/old/generate-images-posts/node_modules/canvas/build/Release --napi_version=8 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v93' (1) npm ERR!...

How to Fix the \"__dirname is not defined in ES module scope\" Error

If you encounter the “__dirname is not defined in ES module scope” error while using __dirname in an ES module, here’s how you can fix it. In a Node script, __dirname is used to retrieve the path of the folder where the current JavaScript file resides. However, it cannot be used within an ES module, leading to the aforementioned error. To overcome this issue, follow these steps: Import the Node.js path module and the fileURLToPath function from the url module: import path from 'path'; import { fileURLToPath } from 'url'; Replicate the functionality of __dirname using the following code: const __filename = fileURLToPath(import....

How to Handle Failed Requests with Axios in Node.js

When using the popular Axios library in Node.js to make network requests, you may encounter a problem where the Node.js process crashes when the request fails. This can be unexpected and frustrating, but fortunately, there is a simple solution. The Issue: Crash on Failed Requests Let’s take a look at some code that uses Axios to make a POST request: axios({ method: 'post', url: 'https://...', data: JSON.stringify({ ... }) }); In this example, we are making a POST request to a specified URL....