Fixing PWA Middleware Issues with nvm on NexJS project in 3 simple steps
Tags: nextjs, pwa, npm, devops
Published: 2023-03-09 13:26:33

So I have been experiencing an issue with PWA middleware whilst trying to work on a NexJS project. so this project was pulled from an organization's repo and I'm to get started with building a deployment pipeline using jenkins and our cloud provider is Microsoft Azure. 

When I pulled my working branch of the repo to my local machine, I had issues of PWA middleware complaining about some modules. Now, let's talk a tiny bit about PWA


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rn3i8qm2stbl0z819kqe.jpeg)

> Photo by James Wiseman on Unsplash

To enhance the functionality of a NextJS PWA, developers use middleware. Middleware in NextJS is code that runs between the server and the application, modifying requests and responses as necessary. PWA middleware is specifically designed to handle the PWA-specific features and requirements.

Issues with PWA Middleware and Node Modules

When working on a NextJS project that uses PWA middleware, it's not uncommon to run into issues with node modules. This is because PWA middleware requires specific modules and dependencies to function correctly. If these modules are not installed or if there are version mismatches, the middleware will fail, causing errors in the application.


If you encounter PWA middleware issues in a NextJS project, there are several steps you can take to resolve them. One common solution is to use Node Version Manager (nvm) to switch between different versions of Node.js.

However, if you've already tried switching node versions with nvm and are still experiencing issues with PWA middleware, you can try the following steps:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0o5338sb61eh10i8q6vs.png)

> Error messages



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dg2y2z4t6vjsd0qwfft2.png)

**Step 1: Check Node and npm Versions**

The first step is to check the versions of Node and npm installed on your machine. You can do this by running the following command in your terminal:

`npm --v
`
Note of the npm version, as this is important for the next step.

**Step 2: Install Node Modules**

Next, try installing the required node modules for the project by running the following command:

`npm i`

If this command fails, do not force the installation or use legacy peer dependencies. Instead, move on to the next step.

**Step 3: Install a Specific Version of npm**

To fix the issue with PWA middleware, you need to install a specific version of npm that matches the version used by other developers who did not encounter the middleware issue. You can do this by running the following command:

`npm install npm@<version number or latest>
`

Replace <version number> with the working npm version used by other developers. Alternatively, you can use the latest keyword to install the latest version of npm.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i0hd2x245nibql1n45ai.png)

**Update (06/04/2023)**
A colleague experience some PWA issue whilst working on their branch, so we had to get on a session to see how I can assist with fixing the PWA issue. 
I discovered an update as to how you get to add PWA features to your Next.js application, such as offline support and the ability to install the app on a user's device (specifically the statement that loads the next-pwa plugin).

So, on the next.config.js file, this line of code:
`const withPWA = require("next-pwa");`

was replaced with this:
`const withPWA = require('next-pwa')({
  dest: 'public'
})`

😇😇😇

**Summary**

PWA middleware is an essential part of building PWAs with NextJS. However, issues with node modules can cause middleware to fail, resulting in errors in the application. If you encounter such issues, you can try switching between different versions of Node.js using nvm or installing a specific version of npm that matches the working version used by other developers. These steps should help you fix the problem and get back to building your NextJS PWA with confidence.