Skip to content

Cannot use a Response.json return as a variable to build in Nextjs 15.3.2 #79088

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

Open
dqfort opened this issue May 12, 2025 · 1 comment
Open

Comments

@dqfort
Copy link

dqfort commented May 12, 2025

Link to the code that reproduces this issue

https://github.com/dqfort/nextjs-build-error

To Reproduce

  1. Create a ts file including this code:
export const ErrorResponse = (statusCode: number, message: string) => {
  return Response.json({
    message
  }, {
    status: statusCode
  });
}

export const unknownError = ErrorResponse(520, "Unknown error.");
  1. Import the file in the project to use the variable.
  2. Run npm run build, but it will fail.

Current vs. Expected behavior

Current result:
Error occurred prerendering page "/test". Read more: https://nextjs.org/docs/messages/prerender-error
ReferenceError: message is not defined

Expected result
Creating an optimized production build ...
✓ Compiled successfully

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.3.0: Thu Jan  2 20:24:23 PST 2025; root:xnu-11215.81.4~3/RELEASE_ARM64_T6020
  Available memory (MB): 65536
  Available CPU cores: 12
Binaries:
  Node: 22.9.0
  npm: 10.8.3
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  next: 15.3.2 // Latest available version is detected (15.3.2).
  eslint-config-next: 15.3.2
  react: 19.1.0
  react-dom: 19.1.0
  typescript: 5.8.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Not sure

Which stage(s) are affected? (Select all that apply)

next build (local)

Additional context

I tested it worked for this dependecies:

"next": "15.0.3",
"react": "^18.1.0",
"react-dom": "^18.0.0"

but not work for this dependecies:

"react": "^19.0.0",
"react-dom": "^19.0.0",
"next": "15.3.2"
@DuckThom
Copy link

DuckThom commented May 15, 2025

I just took a look at this and it appears that the compiled page is not using the renamed message variable and wanted to add some additional information.

The following code from lib/common/http.ts:

export const ErrorResponse = (statusCode: number, message: string) => {
    return Response.json({
        message
    }, {
        status: statusCode
    });
}

Gets compiled into the page (.next/server/app/test/page.js) like this:

9726:(e,t,r)=>{"use strict";r.r(t),r.d(t,{default:()=>o});var s=r(687);Response.json({message},{status:520})

When changing the message line to message: message, the build works and it's compiled like this:

9726:(e,t,r)=>{"use strict";r.r(t),r.d(t,{default:()=>o});var s=r(687);Response.json({message:"Unknown error."},{status:520})

So it looks like the build process is incorrectly processing the message var during compilation. Also checked against Next.js 15.4.0-canary.35 and the issue is still present.

Perhaps also worth noting that this error does not happen with the original code using the experimental turbo compiler.

Output file: .next/static/chunks/eb21986c0b06ee9e.js

let s=(t,e)=>Response.json({message:e},{status:t}),r=s(520,"Unknown error.")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants