Read files in Node.js using callbacks
Callbacks can be used to write clean implementations of anything Promises in Node.js. This is a sample code to read files using callbacks.
How to install a specific nodejs version according to the workspace with pnpm?
You install a particular version of nodejs according to a workspace with pnpm. For installation, you need a .npmrc file. Steps Create a .npmrc file The first step is to create a .npmrc file in the […]
How to read command line arguments in NodeJS?
Let’s assume you are passing a few values from the command line when you are running a node command. These values can be read inside your code using process.argv. All […]
TypeError: Router.use() requires a middleware function but got a Object – How to fix this Express/Node.js error?
TypeError: Router.use() requires a middleware function but got a Object – How to fix this Express/Node.js error? You would typically face this error when you define a new router and […]
How can the default node version be set using NVM?
To view the list of NVM installed in your machine: To make an NVM version default:
SyntaxError: Cannot use import statement outside a module – How to fix this error in Node.js?
SyntaxError: Cannot use import statement outside a module – This error occurs when you use ES6 imports in your node.js code. To fix this just add “type”: “module” to your […]
Convert HTML to Pug in Node.js
How to fix ‘unexpected console statement’ error in Node.js?
To fix the ‘unexpected console statement’ error in Node.js, just create a .eslintrc.js in the directory of the project and put the following configuration in it.
Limit API requests in express.js
In this tutorial we are going to use an npm package called express-rate-limit. Install it using the following command. This library provides a brute-force protection for express routes by rate-limiting […]
Check if a path is file or directory in Node.js
To find out if a file path is a file or a directory we can use fs.statSync(path).isDirectory() method. It returns a boolean, true if the path is a directory.
Get file size in Node.js
To get file size in Node.js, the following code snippet would help. statSync() method from fs package synchronously returns information about the given file path the file size in bytes.
Prevent your node.js server from crashing on production
Detect mobile browsers without user agent string parsing: Client Hints
ua-client-hints is a proposal to detect if a site is visited from Chrome on mobile. It states By default, servers should receive only the user agent’s brand, significant version number, […]
SyntaxError: Cannot use import statement outside a module – node.js error. How to fix?
Since the 14.x version of the Node you can use import to import the modules you need. You can use either require or import. You can’t natively mix them. When […]
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client – How to fix?
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client – This error actually means you have already sent response to the client after a request is made,but […]
Adding custom Chai assertions to your Cypress tests
Cypress allows you to add custom assertions as Chai plugins and use them in global scope. I found a good Chai plugin to compare two objects in deep ignoring the […]
How to make an Axios DELETE request?
Making a DELETE request in Axios is very similar to making a POST or GET request. Let’s assume we are making a DELETE request to delete a user. The endpoint […]
Remove unused npm modules from package.json
Depcheck is a tool for analyzing the dependencies in a project to see: how each dependency is used, which dependencies are useless, and which dependencies are unused from package.json. To identify […]
Add custom CSS to your Swagger documentation
In this post we are going to discuss how to add custom css to your Swagger documentation. I always wanted to change the default navigation bar and the Swagger logo […]
Uploading files in node.js and express using express-fileupload
To handle file uploads in your node.js / express APIs, there are many middleware packages readily available. express-fileupload is one of those simple express middleware available as an npm package. […]
Add JWT Authentication to your Swagger API docs
In this tutorial let’s discuss how to add authentication to your Swagger APIs, specifically JWT. Your APIs should be authenticated with some authentication schemes to secure them from online attacks. […]
Add Swagger documentation to your Express APIs in just five minutes
If you are an API developer there’s no need to introduce Swagger. It’s one of the beautiful and simple ways to document your APIs. Swagger has some fantastic DIY documentation […]
Manage multiple environments in node.js using node config
In this tutorial let’s see how to manage multiple environments in node.js using node config. In backend projects, it’s critical to manage multiple environments when you application reaches production level. […]
How to make an Axios POST request?
In this post let’s take a quick look at how to make a POST request in Node.js with Axios npm package. We have already taken a look at how to […]
How to get node version programmatically?
In this small tutorial, we will learn how to get the version of node programmatically from your Node.js source code. In Node.js, version of the node can be retrieved using […]
Error: ENOSPC: System limit for number of file watchers reached – How to fix this NPM error?
In this post, we will learn how to fix the error when you run npm start command for starting a node.js app – Error: ENOSPC: System limit for number of […]
How to resolve parameterized route conficts in express.js?
In this quick post, we will learn how to resolve route conflicts that arises when we are using parameterized routes in express.js routes. Let’s take two routes as an example. […]
How to get query strings and path parameters in Express.js?
In this article we will learn how to extract values from query strings and path parameters from an Express.js request. In Express.js routes / services we needs to get values […]
How to intercept all requests in Express + Node.js?
In this quick post, you will learn how to intercept all requests in Express.js. Learn how to write an interceptor function to intercept all requests. You can use the following […]
JsonWebTokenError: invalid signature while verifying token using JWT
In this quick post, we will see how to fix JsonWebTokenError: invalid signature error when you are implementing JSON Web Token in node.js and Java. I’m using node.js example. I […]
How to get request path from an Express request?
In this small post, let’s have a look at how you can extract the request path from an Express request object. Let’s assume you are building an API with Express […]
How to Dockerize your node.js application?
In this tutorial you will learn how to containerize/dockerize your existing node js application with Docker. You will also learn how to add instructions to a Dockerfile to dockerize a […]
Validating request in Express using express-validator
In this article you will learn how to validate your payload coming from a POST request for an Express API using express-validator. Learn how to validate any data coming in […]
Pass arguments to your node.js app from pm2
In this post you will learn how to pass arguments to your node.js when you use pm2 to run your node.js application. You can pass arguments to your node.js app […]
How to uninstall npm modules in node.js?
In this small post, find out how to uninstall npm packages from a node.js project or globally. Any npm package can be installed easily using the npm install command. But […]
Proxying npm packages using Nexus
In this post, you will learn how to use your Nexus npm proxy repository as a proxy for your node js project’s npm packages. You also learn how to concfigure […]
How to configure npm with npmrc?
In this article, we will discuss the basics of npm configuration file called npmrc and the various ways npmrc can be configured. One of the most important features of the […]
Keep your Node.js app running in the background using PM2
Get started with the basics of PM2 and learn how to run your Node.js apps in the background using Node.js process manager PM2. PM2 is a daemon process manager that […]
File downloads using node.js and Express
In this article, we will quickly go through how to send files in the response in an Express JS application that can be downloaded and saved by the browser. Express.js […]
Update an Excel file in node.js
In this post, you will learn how to update a Microsoft Excel spreadsheet in node.js. We have already seen how to read an excel file in node.js using the npm […]
Real an Excel file in node.js
In this post, you will learn how to read a Microsoft excel file (.xls, .xlsx) in node.js. Microsoft Excel is the widely used spreadsheet application whose file extensions are .xls, […]
Scrape a web page in node.js using Cheerio
Learn how to scrap a simple web page in node.js using Cheerio and Axios node modules. I was developing a widget in html to show weather information based on a […]
Missing write access to /usr/local/lib/node_modules – npm error – How to fix?
When you are setting up npm packages for the first time for a Node JS project, you might face a strange error – Missing write access to /usr/local/lib/node_modules. This error […]
Update all the Node dependencies to their latest version
How do you update all the npm dependencies in the package.json file, to their latest available versions? Warning: Updating the package versions to the latest ones might break your code. […]
Generate UUID in Node JS
There are multiple ways to generate random UUID in Node JS. The fastest possible way to create random 32-char string in Node is by using native crypto module. Let’s see one by […]
Install NodeJS on Ubuntu 19.04
In this tutorial, I’m going to show you how to install Node.js on Ubuntu 19.04 / 18.04. Once you are done, you’ll have Node.js installed and be ready to start […]
TypeError: Cannot read property ‘findOneAndUpdate’ of undefined – Node Agenda error – How to fix?
The Agenda constructor is asynchronous, and hence you can’t trigger a job without the agenda being started. You can’t run or schedule a job before agenda has built a connection with MongoDB […]
Blocked by CORS policy error – How to fix?
Cross-Origin Resource Sharing (CORS) is a technique that makes use of additional HTTP headers to tell browsers to give a web application running at one origin, access to selected resources […]
MongoDB uses ISODate as their primary date type. If you want to insert a date object into a MongoDB collection, you can use the Date() shell method. You can specify […]
Pagination using Node.js and MongoDB
A huge set of data can be split into a series of related pages by Pagination and only one page can be loaded and viewed at a time. If you […]