question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Getting "ReferenceError: Response is not defined" while trying to upgrade an app from next 11 to next 12

See original GitHub issue

What version of Next.js are you using?

12.0.0

What version of Node.js are you using?

16.3.0

What browser are you using?

Chrome

What operating system are you using?

macOS

How are you deploying your application?

next build + next start

Describe the Bug

I am trying to upgrade Next.js version in a big application from v11.0.1 to Next.js 12. It uses Next with express.js. I’m getting the following error:

ReferenceError: Response is not defined
    at Object.<anonymous> (/Users/andreisena/projects/.../node_modules/next/dist/server/web/spec-extension/response.js:23:28)
    at Module._compile (node:internal/modules/cjs/loader:1109:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1138:10)
    at Module.load (node:internal/modules/cjs/loader:989:32)
    at Function.Module._load (node:internal/modules/cjs/loader:829:14)
    at Function.wrappedLoad [as _load] (/Users/andreisena/projects/.../node_modules/newrelic/lib/shimmer.js:373:24)
    at Module.require (node:internal/modules/cjs/loader:1013:19)
    at require (node:internal/modules/cjs/helpers:93:18)
    at Object.<anonymous> (/Users/andreisena/projects/.../node_modules/next/dist/server/next-server.js:47:17)
    at Module._compile (node:internal/modules/cjs/loader:1109:14)

Expected Behavior

To work correctly like v11

To Reproduce

I couldn’t reproduce it yet on a fresh install of Next.js and I cannot share the repo since it is private. But if I find something new I will post here.

What I could see in a fresh install is that when I debug Response in server/web/spec-extension/response.js, it has the same contract as node-fetch Response. Is that correct?

Object [Response] {
  url: [Getter],
  status: [Getter],
  ok: [Getter],
  redirected: [Getter],
  statusText: [Getter],
  headers: [Getter],
  clone: [Function: clone],
  body: [Getter],
  bodyUsed: [Getter],
  arrayBuffer: [Function: arrayBuffer],
  blob: [Function: blob],
  json: [Function: json],
  text: [Function: text],
  buffer: [Function: buffer],
  textConverted: [Function: textConverted]
}

By the way, on the main application, Response always come as undefined.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
javivelascocommented, Nov 22, 2021

By default Next will polyfill some fetch APIs (Request, Response, Header and fetch) only if fetch is not found in the global scope in certain entry points. If we have a custom server which is adding a global fetch (and only fetch) at the very top then the rest of APIs will not be polyfilled. We can reproduce it with the minimal Express example:

global.fetch = async function () {}

const { createServer } = require('http')
const { parse } = require('url')
const next = require('next')

const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()

app.prepare().then(() => {
  createServer((req, res) => {
    // Be sure to pass `true` as the second argument to `url.parse`.
    // This tells it to parse the query portion of the URL.
    const parsedUrl = parse(req.url, true)
    const { pathname, query } = parsedUrl

    if (pathname === '/a') {
      app.render(req, res, '/a', query)
    } else if (pathname === '/b') {
      app.render(req, res, '/b', query)
    } else {
      handle(req, res, parsedUrl)
    }
  }).listen(3000, (err) => {
    if (err) throw err
    console.log('> Ready on http://localhost:3000')
  })
})

With the introduction of Middleware, all web code must run in a Sandbox outside of the Node main process where we inject all polyfills. Right now we are using one dependency in the server that should not be there. I’m working on a small refactor to fix this case.

4reactions
gm-rebrickercommented, Oct 30, 2021

NodeJS Version: 14.17.6

I have a similar Issue. I call “axios.request(<axiosRequestArgs>)” on the client side which should return an AxiosPromise. This works fine on next@11.1.2. When I upgrade next to 12.0.0 / 12.0.1 I get the following error when I try to resolve the promise: “TypeError: Cannot read properties of undefined (reading ‘then’)” The request isn’t made at all, since it don’t appear in the network tab of the Chrome Devtools.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Response is not defined (trying to upgrade from NextJS 11 to ...
Hi all,. I'm trying to upgrade our project from Next 11 to 12. However, i'm unable to get our custom server to start...
Read more >
Why am I getting ReferenceError: Response is not defined?
I am trying to set up a basic server, but have no idea why I am getting a response error. var http =...
Read more >
Upgrade Guide - Next.js
Upgrade Guide. Upgrading from 12 to 13. To update to Next.js version 13, run the following command using your preferred package manager:
Read more >
ReferenceError: response is not defined (Example) - Treehouse
ReferenceError : response is not defined. When go to the server I am getting the following error: Server running at http://<workspace-url>/ ...
Read more >
How to Fix Uncaught ReferenceError: $ is not defined in jQuery
If you're still getting this error there might be some other reasons, for example, if you have a local copy of the jQuery...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found