Request is not defined, Error - NextJs

Hi! I created a new project with npx create-next-app@latest and then immediately ran npm run dev.

I got the following error:

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

So then I uninstalled all my node versions from my computer and reinstalled the latest one v20 using nvm. I also created another next app with npx create-next-app@13.4. Which was the one that mosh used. However I continue to get the same error.

My guess is that there is some version of node 17.4 in my machine and nextjs can’t detect the newest one I just installed.

Does anyone know how I can solve this?

1 Like

I was able to solve it and here is the solution in case anyone has the same problem:

I tried all solutions from the following discussion however none of the solutions were able to solve it for me since whenever I was running Nextjs it would still think I had an older version of Node.

I was able to solve the problem by uninstalling node and npm completly from my machine using this article. I tried to uninstall nvm but it worked without uninstalling it.

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

Hey Man,

I got the same error today, And I was able to solve it by installing node to latest version “npm i node@latest”

This is where i got the solution from - reactjs - NextJS - ReferenceError: Request is not defined | Reading old version of Node [solved] - Stack Overflow

I’m using a Macbook running Sonomo OS