How to Use or Execute a Package Installed Using npm

Learn how to include and use a package that you have installed using npm into your Node.js code. When you install a package using npm, it gets saved into your node_modules folder. But how do you actually use it in your code? Let’s say you have installed the popular JavaScript utility library lodash using the following command: npm install lodash This command will install the lodash package in your local node_modules folder....

How to Use pm2 to Serve a Node.js App

In this tutorial, we will explore how to use pm2, a powerful process management tool for Linux, to run and manage a Node.js app on a DigitalOcean VPS. Additionally, we will set up pm2 to automatically update and restart the app whenever changes are pushed to its corresponding GitHub repository. To begin, make sure you have signed up for a DigitalOcean account and created a VPS using the NodeJS image, which comes pre-installed with pm2 and Node....

How to Use Promises and Await with Node.js Callback-Based Functions

In the world of Node.js, many APIs and libraries were built before the introduction of promises. As a result, they rely on a callback-based solution. However, working with nested callbacks can lead to a complex and messy code structure commonly known as “callback hell.” Thankfully, there is a solution: the use of promises and the await keyword. To remove callbacks and make use of promises, Node.js provides a useful utility called promisify from the util module....

How to Use Redis with Node.js

One of the most popular libraries for working with Redis server in a Node.js application is node-redis, which can be found at https://github.com/NodeRedis/node-redis. To install the library in your project, run the following command: npm install redis Tip: If your project is brand new and does not have a package.json file, make sure to run npm init -y first. Connecting to the Redis Instance Once the library is installed, require it in your project using:...

How to Use the Node.js fs Module with async/await

Node.js built-in modules are well-known for not being promise-based. This was because these modules were created before promises became popular. Although the promisify function has been available for some time, Node.js introduced a new promise-based API starting from version 10. Currently, this new API is only available for the fs built-in module, and it remains uncertain whether it will be implemented for other native modules in the future. To utilize this new API, follow these steps:...

How to Use the Node.js REPL

The Node.js REPL (Read-Evaluate-Print-Loop) is a powerful tool for quickly exploring and testing Node.js features. This guide will show you how to use the Node.js REPL effectively. Running the Node.js REPL To start the Node.js REPL, open your terminal and execute the node command without specifying a file: node This will start the REPL, and you’ll see a prompt (>). The REPL is now ready to accept and evaluate JavaScript code....

How to Write a CSV File with Node.js

In this tutorial, we will learn how to write an array of data to a CSV file using Node.js. We will be using the objects-to-csv library, which is a great tool for quickly writing an array of objects to a CSV file. To get started, let’s install the objects-to-csv library by running the following command in your Node.js project folder: npm install objects-to-csv Once the library is installed, we can require it in our Node....

How to Write a JSON Object to File in Node.js

Learn how to save a JSON object to a file in Node.js and retrieve it later. Storing data in the filesystem can be a reliable approach for Node.js applications. If you have an object that can be serialized to JSON, you can follow these steps to save it to a file: const fs = require('fs'); const storeData = (data, path) => { try { fs.writeFileSync(path, JSON.stringify(data)); } catch (err) { console....

I Purchased bootcamp.dev: A New Home for My Web Development Bootcamp

Every Spring, I organize a highly anticipated course called the Web Development Bootcamp. This comprehensive 20-week program covers all the essential aspects of web development, including vanilla JavaScript, React, Node.js, Next.js, and more. The course has been a tremendous success, with a growing number of signups and consistently positive outcomes for my students. Due to its popularity and effectiveness, I have decided to make it an annual event. Formerly known as the JavaScript Full-Stack Bootcamp, I recently rebranded it to better reflect the diverse skills and technologies covered....

Interacting with File Descriptors in Node

Learn how to efficiently interact with file descriptors in Node.js. In order to work with files in your filesystem using Node.js, you need to obtain a file descriptor. A file descriptor can be obtained by opening a file using the open() method provided by the fs module: const fs = require('fs'); fs.open('/Users/flavio/test.txt', 'r', (err, fd) => { // fd is the file descriptor }); In the above code, the second parameter 'r' signifies that the file should be opened for reading....