-
Notifications
You must be signed in to change notification settings - Fork 27.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The usage of NextResponse.json() function leads to the error TypeError: Response.json is not a function. in dev mode #48524
Comments
I am also experiencing this issue in version
|
may be related: |
I have the same problem when using |
I ran into this issue while updating one of our apps to use App Router. The source of the problem in our case turned out to be that we explicitly disabled # before
NODE_OPTIONS='--no-experimental-fetch' next dev
# after
next dev After removing the |
I am facing the same issue even in next dev. I am getting the following error: TypeError: next_dist_server_web_exports_next_response__WEBPACK_IMPORTED_MODULE_2__.default.json is not a constructor |
I initially had the same problem in my environment, but by upgrading the Node.js version from 18.0.0 to 18.16.0, the latest stable version, the error no longer occurred. |
I'm getting this error while trying to implement NextJS' own exmple:
with Node v18.2.0 / Mac M1 |
use this: |
Next.js relies on the runtime environment to provide the Node.js 18.2 did not have this method yet, it was added in 18.3. See: nodejs/node#43197 (comment) You can upgrade your runtime environment to at least 18.3 and it should fix this issue. |
Please re-open this issue, as updating to Node does not resolve it:
... try to hit API ...
|
P.S. When I tried the variation @hj007jacky suggested:
I get a similar but different error about NextResponse not being a constructor:
And all this is with the latest version of Next:
|
I did manage to make things work with:
So it's clearly a problem with |
I can confirm it's working with node 20.3.1 and next 13.4.6. |
It's not working for me in jest. I have this function:
and this unit test:
The function is working correctly via I tried node version My
What am I doing wrong? |
I'm pretty sure it has to do with this line: next.js/packages/next/src/server/web/spec-extension/response.ts Lines 74 to 75 in b714ac5
|
If anyone else comes here: I found what my issue was. It was the The solution was just to remove that import once I was on node |
My issue was due to json static method was not available in the target browsers like Firefox and Safari: @hj007jacky solution works like a charm =) |
I'm found the problem: it's an import issue. When I have this, I get the error:
But when I change the import like so, it works:
So it appears that some sort of export-related issue causes this problem ... for users like me, but not for other users (and I'm not sure what's different about my environment). P.S. If I import both, and log them side-by-side ...
They appear to be identical:
... but again, |
I had this issue and none of the above fixes worked. Turns out I just needed to install See here |
If we need to upgrade node version for this to work it does but vercel only has up to node 18 so what is the solution for a deployed version? Tried with these and can't get it working locally error NotSupportedError: multipart/form-data not supported error TypeError: Response.json is not a function `
} |
Ran into this problem earlier today, updating my Node.js to This runtime env dependency (at least Node.js v 18.3) should be outlined further in the docs, IMO. |
I made a package that exports some utility functions for making route handlers. Those functions use app using nodejs runtime
app using edge runtime
No idea why it was doing that, but I was able to fix it by adding a file like this: // ./next-server-bugfix.ts
import {
NextRequest as NextRequest1,
NextResponse as NextResponse1,
} from "next/server";
export type NextRequest = NextRequest1;
const NextResponseBase = NextResponse1["default"] || NextResponse1;
export class NextResponse extends NextResponseBase {} and updating all my imports within my package to from |
This worked for me! Thank you |
It's not working for me when I add |
I suppose the ticket can be reopened because the issue is stil there when project is deployed to the Vercel (even thought the Node.js version on Vercel is set to 18.x).
The temporary fix by @mjewell worked for me, thank you! |
Please reopen. I've also ran into a similar issue with
|
I have the same error when trying to do a simple upload with
|
This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Verify canary release
Provide environment information
Which area(s) of Next.js are affected? (leave empty if unsure)
App directory (appDir: true), Middleware / Edge (API routes, runtime)
Link to the code that reproduces this issue
https://github.com/aleksFedotov/next-js-error
To Reproduce
npm install
npm run dev
Describe the Bug
When using NextResponse.json() in development mode, an error occurs.
error - TypeError: Response.json is not a function
at Function.json (webpack-internal:///(sc_server)/./node_modules/next/dist/server/web/spec-extension/response.js:67:35)
at GET (webpack-internal:///(sc_server)/./app/api/hello/route.ts:8:95)
at eval (webpack-internal:///(sc_server)/./node_modules/next/dist/server/future/route-modules/app-route/module.js:232:24)
at eval (webpack-internal:///(sc_server)/./node_modules/next/dist/server/lib/trace/tracer.js:108:36)
at NoopContextManager.with (webpack-internal:///(sc_server)/./node_modules/next/dist/compiled/@opentelemetry/api/index.js:360:30)
at ContextAPI.with (webpack-internal:///(sc_server)/./node_modules/next/dist/compiled/@opentelemetry/api/index.js:30:58)
at NoopTracer.startActiveSpan (webpack-internal:///(sc_server)/./node_modules/next/dist/compiled/@opentelemetry/api/index.js:953:34)
at ProxyTracer.startActiveSpan (webpack-internal:///(sc_server)/./node_modules/next/dist/compiled/@opentelemetry/api/index.js:993:36)
at eval (webpack-internal:///(sc_server)/./node_modules/next/dist/server/lib/trace/tracer.js:97:107)
at NoopContextManager.with (webpack-internal:///(sc_server)/./node_modules/next/dist/compiled/@opentelemetry/api/index.js:360:30)
at ContextAPI.with (webpack-internal:///(sc_server)/./node_modules/next/dist/compiled/@opentelemetry/api/index.js:30:58)
at NextTracerImpl.trace (webpack-internal:///(sc_server)/./node_modules/next/dist/server/lib/trace/tracer.js:97:32)
at eval (webpack-internal:///(sc_server)/./node_modules/next/dist/server/future/route-modules/app-route/module.js:227:49)
at AsyncLocalStorage.run (node:async_hooks:327:14)
at Object.wrap (webpack-internal:///(sc_server)/./node_modules/next/dist/server/async-storage/static-generation-async-storage-wrapper.js:37:24)
at eval (webpack-internal:///(sc_server)/./node_modules/next/dist/server/future/route-modules/app-route/module.js:181:93)
at AsyncLocalStorage.run (node:async_hooks:327:14)
at Object.wrap (webpack-internal:///(sc_server)/./node_modules/next/dist/server/async-storage/request-async-storage-wrapper.js:64:24)
at AppRouteRouteModule.execute (webpack-internal:///(sc_server)/./node_modules/next/dist/server/future/route-modules/app-route/module.js:180:87)
at AppRouteRouteModule.handle (webpack-internal:///(sc_server)/./node_modules/next/dist/server/future/route-modules/app-route/module.js:272:41)
at RouteHandlerManager.handle (C:\Coding\practice\error\node_modules\next\dist\server\future\route-handler-managers\route-handler-manager.js:28:29)
at doRender (C:\Coding\practice\error\node_modules\next\dist\server\base-server.js:919:58)
at cacheEntry.responseCache.get.incrementalCache.incrementalCache (C:\Coding\practice\error\node_modules\next\dist\server\base-server.js:1130:34)
at C:\Coding\practice\error\node_modules\next\dist\server\response-cache\index.js:96:42
at ResponseCache.get (C:\Coding\practice\error\node_modules\next\dist\server\response-cache\index.js:144:11)
at DevServer.renderToResponseWithComponentsImpl (C:\Coding\practice\error\node_modules\next\dist\server\base-server.js:1049:53)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async DevServer.renderPageComponent (C:\Coding\practice\error\node_modules\next\dist\server\base-server.js:1270:24)
at async DevServer.renderToResponseImpl (C:\Coding\practice\error\node_modules\next\dist\server\base-server.js:1301:32)
at async DevServer.pipeImpl (C:\Coding\practice\error\node_modules\next\dist\server\base-server.js:619:25)
at async Object.fn (C:\Coding\practice\error\node_modules\next\dist\server\next-server.js:1126:21)
at async Router.execute (C:\Coding\practice\error\node_modules\next\dist\server\router.js:311:32)
at async DevServer.runImpl (C:\Coding\practice\error\node_modules\next\dist\server\base-server.js:593:29)
at async DevServer.run (C:\Coding\practice\error\node_modules\next\dist\server\dev\next-dev-server.js:922:20)
at async DevServer.handleRequestImpl (C:\Coding\practice\error\node_modules\next\dist\server\base-server.js:528:20)
However, in production mode, there is no such error. Also, if you use this function on Linux instead of Windows 11, the error disappears.
Expected Behavior
Produce a response with the given JSON body.
Which browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
The text was updated successfully, but these errors were encountered: