NextJS - ReferenceError: Request is not defined | Reading old version of Node
Asked Answered
E

7

9

When I run npm run dev in my nextjs project I get the following error:

.../node_modules/next/dist/server/web/spec-extension/request.js:28
class NextRequest extends Request {
                          ^

ReferenceError: Request is not defined
    at Object.<anonymous> (.../node_modules/next/dist/server/web/spec-extension/request.js:28:27)
    at Module._compile (node:internal/modules/cjs/loader:1097:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:999:19)
    at Module.mod.require (.../node_modules/next/dist/server/require-hook.js:64:28)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (.../node_modules/next/dist/server/web/spec-extension/adapters/next-request.js:37:18)
    at Module._compile (node:internal/modules/cjs/loader:1097:14)

Node.js v17.4.0

When I run node -v I get:

v20.9.0

I have already uninstalled node from my machine and reinstalled it using nvm.

From what I understood nextjs and the packages I have installed are complaining because I am not using the required node version. However since in my terminal when I run node -v I get that I am using v20.9 I'm not sure why else Nextjs isn't detecting the correct node version

Anyone know how I can solve this?

Extenuate answered 29/10, 2023 at 15:21 Comment(0)
M
27

Open terminal inside your project and then run below mentioned command

npm i node@lts

Molotov answered 2/11, 2023 at 13:9 Comment(2)
This does not work on a pc using v 16.15.1 I observed that it works for all versions of node greater that 18.Nork
I says No matching version found forAnalects
M
10

I got this issue when using a node version below 18 which is actually required by nextJs 13. If you are not using node version manager, I suggest you install it you can find details about how to install it here

Then run nvm install 18 this will install and set your current version to 18. Then just run yarn dev or npm run dev to start your development server.

Maritsa answered 7/11, 2023 at 14:36 Comment(0)
C
3

Install the latest node LTS version

For windows download latest LTS version from https://nodejs.org/en For linux or mac use nvm https://github.com/nvm-sh/nvm

Chryselephantine answered 3/11, 2023 at 14:53 Comment(0)
E
1

I was able to solve the problem by uninstalling node and npm completely from my machine using the answer to this question.

I then installed node and npm again using their official website.

Extenuate answered 30/10, 2023 at 12:59 Comment(0)
C
1

Using the latest version or changing node version by NVM will solve the problem

Cholinesterase answered 3/11, 2023 at 19:45 Comment(0)
M
1

Please try upgrading your node to 18.17.0 or higher package.json on next package

Matherne answered 15/11, 2023 at 2:51 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Homocercal
A
0

It happened to me. I was working on 2 projects one was in React and the other one was in nextJs. For react project node 16 was used and 20 for nextjs project.

I faced this error because when I was running the next app, I was using node 16. So I just changed the node version to 20 and restarted the app. It started working fine.

So finally that was the issue of the node version. Just type

nvm use 20

if it's not installed?

nvm install 20

and then shift to node 20

Now restart your project and it will work.

Awe answered 24/7 at 15:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.