[Fixed] NextJS – ReferenceError: Request is not defined | Reading old version of Node [solved] – Reactjs

by
Ali Hasan
next.js node.js npm nvm reactjs

Quick Fix: Install an older version of Node like LTS(Long Term Support) on your system. This can be done by running npm i node@lts in your terminal.

The Problem:

A user is attempting to run npm run dev in their Next.js project but encounters the error ReferenceError: Request is not defined. Upon investigation, the user discovers that they have Node.js version 20.9.0 installed despite Next.js requiring a specific version. The user has tried uninstalling and reinstalling Node.js using NVM (Node Version Manager) but the issue persists. Determine the cause of the error and provide a solution to resolve the version conflict between Next.js and the installed Node.js version.

The Solutions:

Solution 1: Upgrade Node to the LTS version

To resolve this issue, you need to upgrade Node.js to the LTS (Long-Term Support) version. The LTS version is a stable and supported version of Node.js that is recommended for production use. Here’s how you can do it:

  1. Open a terminal window inside your project directory.
  2. Run the following command to install the LTS version of Node.js:
  3. npm i node@lts
    
  4. Once the installation is complete, verify the Node.js version by running the following command:
  5. node -v
    
  6. Make sure that the output of the command shows the LTS version of Node.js (currently, it’s v18.12.1).
  7. Restart your Next.js project by running the following command:
  8. npm run dev
    
  9. Check if the error is resolved and your Next.js project runs without issues.

By upgrading to the LTS version of Node.js, you ensure that your project is using a stable and supported version that is compatible with the requirements of Next.js and the packages you have installed.

\n

Solution 2: Use a Compatible Node Version

\n

This issue can arise due to using an incompatible Node.js version with Next.js. Next.js requires Node.js version 18 or higher, but you mentioned that you’re using Node.js version 20.9.0. To resolve this issue, follow these steps:

  1. Install Node Version Manager (nvm): If you don’t already have nvm installed, install it using the instructions provided here.

  2. Set Node.js Version to 18: Use the following command to install and set Node.js version 18 as your current version:

    nvm install 18
    
  3. Restart Development Server: After setting the Node.js version to 18, restart your development server by running:

    yarn dev
    

    or

    npm run dev
    

This should resolve the "ReferenceError: Request is not defined" error and allow you to run your Next.js project without encountering the issue.

Solution 4: Re-install Node and npm completely

The problem and its associated error message indicate that the issue originates from a version mismatch between the required Node Environment and the currently installed one.

To resolve this issue, completely uninstall Node and npm from your computer using these steps:

  1. Open Command Prompt or Terminal in your system and run the following command to list all Node versions installed:
  2. nvm ls
  3. Identify the version that needs to be uninstalled and use this command to remove it:
  4. nvm uninstall version_number
  5. Once all desired Node versions are uninstalled, uninstall npm using this command:
  6. npm uninstall -g npm

After the uninstallation process is complete, reinstall Node.js and npm by following these steps:

  1. Head over to the official Node.js [website](https://nodejs.org/en/download).
  2. Download the latest LTS (Long Term Support) version of Node.js for your operating system.
  3. Once the download is complete, install Node.js using the downloaded installer.
  4. After Node.js is installed, open a Command Prompt or Terminal and check the installed Node version with this command:
  5. node -v
  6. If the output matches the latest LTS version, Node.js is successfully installed.
  7. To reinstall npm, run this command:
  8. npm install -g npm

Once you have completed these steps, your Node.js and npm should be updated to the latest versions. Try running npm run dev again in your Next.js project. The error related to Request is not defined should be resolved.

Solution 5: Upgrade Node.js to a Supported Version

Your Next.js project is likely using a newer version of Node.js than the one you have installed on your system. The Next.js documentation specifies that Node.js 18.17.0 or higher is required. To resolve this issue, follow these steps:

  1. Check the version of Node.js installed on your system by running the command node -v.

  2. If the installed version is lower than 18.17.0, upgrade it to the latest stable version. You can use a Node.js version manager like nvm or the Node.js installer to manage different versions of Node.js on your system.

  3. Once you have upgraded Node.js, verify that the new version is being used by running node -v again.

  4. Restart your Next.js project by running npm run dev or yarn dev.

  5. Check if the ‘ReferenceError: Request is not defined’ error has been resolved.

Q&A

What command can I run to install the latest LTS version of Node?

npm i node@lts

What Node version should I use with NextJS 13?

Node version 18 or higher

How can I update my node version?

You can use a node version manager like nvm to update your node version.

Video Explanation:

The following video, titled "How to fix 'Module not found: Can't resolve 'http' in ...' error with FCL ...", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

Basically, just change 'react-scripts' to 4.0.2 in your package.json and run `npm install` again 😀 Follow me on Twitter: ...