nextjs - ✅(Solved) Fix experimental-build-mode compile outputs invalid code [5 pull requests, 1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
vercel/next.js#83863Fetched 2026-04-08 02:21:19
View on GitHub
Comments
1
Participants
2
Timeline
16
Reactions
2
Author
Timeline (top)
cross-referenced ×5referenced ×5labeled ×2subscribed ×2

Error Message

alessio@Alessio-Gravili's-MacBook-Pro-(16-inch,-2024)-M4-Max cissue % pnpm next build --experimental-build-mode generate ▲ Next.js 15.5.3

  • Environments: .env

Creating an optimized production build ... ✓ Collecting page data
Inlining static env ... Error occurred prerendering page "/". Read more: https://nextjs.org/docs/messages/prerender-error SyntaxError: Invalid left-hand side in assignment at <unknown> (.next/server/app/page.js:1169) Export encountered an error on /page: /, exiting the build. ⨯ Next.js build worker exited with code: 1 and signal: null

Fix Action

Fixed

PR fix notes

PR #13828: templates: fix experimental-build-mode compile generating invalid code

Description (problem / solution / changelog)

When using pnpm next build --experimental-build-mode compile in our website template, Next.js outputs the following code for the getServerSideURL function:

<img width="2322" height="1262" alt="Screenshot 2025-09-16 at 14 39 31@2x" src="https://github.com/user-attachments/assets/bb7dc5c1-ffe7-4ee7-8864-19cb2147c07d" />

If you then run pnpm next build --experimental-build-mode generate or pnpm next build --experimental-build-mode generate-env which inlines the env variable, it becomes the following code:

<img width="2276" height="1340" alt="Screenshot 2025-09-16 at 14 40 14@2x" src="https://github.com/user-attachments/assets/6fc8cbd4-4df8-4d9b-a3e5-fc299e147537" />

Suddenly, it no longer assigns to a variable but to a string, which is invalid JavaScript syntax, throwing the following error:

<img width="2314" height="320" alt="Screenshot 2025-09-16 at 14 40 53@2x" src="https://github.com/user-attachments/assets/2c3487be-82e3-4043-aa04-0d98f0a05b4b" />

This PR works around this issue changing up the source code in getServerSideURL.

I have reported this issue to Next.js: https://github.com/vercel/next.js/issues/83863


Changed files

  • templates/website/src/utilities/getURL.ts (modified, +6/-11)
  • templates/with-vercel-website/src/utilities/getURL.ts (modified, +6/-11)

PR #709: chore(templates): fix experimental-build-mode compile generating inva…

Description (problem / solution / changelog)

…lid code

When using pnpm next build --experimental-build-mode compile in our website template, Next.js outputs the following code:

<img width="2322" height="1262" alt="Screenshot 2025-09-16 at 14 39 31@2x" src="https://github.com/user-attachments/assets/bb7dc5c1-ffe7-4ee7-8864-19cb2147c07d" />

If you then run pnpm next build --experimental-build-mode generate or pnpm next build --experimental-build-mode generate-env which inlines the env variable, it becomes the following code:

<img width="2276" height="1340" alt="Screenshot 2025-09-16 at 14 40 14@2x" src="https://github.com/user-attachments/assets/6fc8cbd4-4df8-4d9b-a3e5-fc299e147537" />

Suddenly, it no longer assigns to a variable but to a string, which is invalid JavaScript syntax, throwing the following error:

<img width="2314" height="320" alt="Screenshot 2025-09-16 at 14 40 53@2x" src="https://github.com/user-attachments/assets/2c3487be-82e3-4043-aa04-0d98f0a05b4b" />

This PR works around this issue changing up the source code in getServerSideURL.

I have reported this issue to Next.js: vercel/next.js#83863

https://github.com/payloadcms/payload/commit/ba2ab2f31d4ceb16c89722d6fb14f88528ed0f1a#diff-ff7aaa6e6d3eef2acc79c0e4fa312431d13f7025391b787aa2b652eac55093ef

Changed files

  • src/utilities/get-url/index.ts (modified, +6/-11)

PR #364: chore(templates): fix experimental-build-mode compile generating inva…

Description (problem / solution / changelog)

…lid code

When using pnpm next build --experimental-build-mode compile in our website template, Next.js outputs the following code:

<img width="2322" height="1262" alt="Screenshot 2025-09-16 at 14 39 31@2x" src="https://github.com/user-attachments/assets/bb7dc5c1-ffe7-4ee7-8864-19cb2147c07d" />

If you then run pnpm next build --experimental-build-mode generate or pnpm next build --experimental-build-mode generate-env which inlines the env variable, it becomes the following code:

<img width="2276" height="1340" alt="Screenshot 2025-09-16 at 14 40 14@2x" src="https://github.com/user-attachments/assets/6fc8cbd4-4df8-4d9b-a3e5-fc299e147537" />

Suddenly, it no longer assigns to a variable but to a string, which is invalid JavaScript syntax, throwing the following error:

<img width="2314" height="320" alt="Screenshot 2025-09-16 at 14 40 53@2x" src="https://github.com/user-attachments/assets/2c3487be-82e3-4043-aa04-0d98f0a05b4b" />

This PR works around this issue changing up the source code in getServerSideURL.

I have reported this issue to Next.js: vercel/next.js#83863

https://github.com/payloadcms/payload/commit/ba2ab2f31d4ceb16c89722d6fb14f88528ed0f1a#diff-ff7aaa6e6d3eef2acc79c0e4fa312431d13f7025391b787aa2b652eac55093ef

Changed files

  • apps/web/src/utilities/get-url/index.ts (modified, +6/-11)

PR #884: chore(templates): fix experimental-build-mode compile generating inva…

Description (problem / solution / changelog)

…lid code

When using pnpm next build --experimental-build-mode compile in our website template, Next.js outputs the following code:

<img width="2322" height="1262" alt="Screenshot 2025-09-16 at 14 39 31@2x" src="https://github.com/user-attachments/assets/bb7dc5c1-ffe7-4ee7-8864-19cb2147c07d" />

If you then run pnpm next build --experimental-build-mode generate or pnpm next build --experimental-build-mode generate-env which inlines the env variable, it becomes the following code:

<img width="2276" height="1340" alt="Screenshot 2025-09-16 at 14 40 14@2x" src="https://github.com/user-attachments/assets/6fc8cbd4-4df8-4d9b-a3e5-fc299e147537" />

Suddenly, it no longer assigns to a variable but to a string, which is invalid JavaScript syntax, throwing the following error:

<img width="2314" height="320" alt="Screenshot 2025-09-16 at 14 40 53@2x" src="https://github.com/user-attachments/assets/2c3487be-82e3-4043-aa04-0d98f0a05b4b" />

This PR works around this issue changing up the source code in getServerSideURL.

I have reported this issue to Next.js: vercel/next.js#83863

https://github.com/payloadcms/payload/commit/ba2ab2f31d4ceb16c89722d6fb14f88528ed0f1a#diff-ff7aaa6e6d3eef2acc79c0e4fa312431d13f7025391b787aa2b652eac55093ef

Changed files

  • src/utilities/get-url/index.ts (modified, +6/-11)

PR #644: chore(templates): fix experimental-build-mode compile generating inva…

Description (problem / solution / changelog)

…lid code

When using pnpm next build --experimental-build-mode compile in our website template, Next.js outputs the following code:

<img width="2322" height="1262" alt="Screenshot 2025-09-16 at 14 39 31@2x" src="https://github.com/user-attachments/assets/bb7dc5c1-ffe7-4ee7-8864-19cb2147c07d" />

If you then run pnpm next build --experimental-build-mode generate or pnpm next build --experimental-build-mode generate-env which inlines the env variable, it becomes the following code:

<img width="2276" height="1340" alt="Screenshot 2025-09-16 at 14 40 14@2x" src="https://github.com/user-attachments/assets/6fc8cbd4-4df8-4d9b-a3e5-fc299e147537" />

Suddenly, it no longer assigns to a variable but to a string, which is invalid JavaScript syntax, throwing the following error:

<img width="2314" height="320" alt="Screenshot 2025-09-16 at 14 40 53@2x" src="https://github.com/user-attachments/assets/2c3487be-82e3-4043-aa04-0d98f0a05b4b" />

This PR works around this issue changing up the source code in getServerSideURL.

I have reported this issue to Next.js: vercel/next.js#83863

https://github.com/payloadcms/payload/commit/ba2ab2f31d4ceb16c89722d6fb14f88528ed0f1a#diff-ff7aaa6e6d3eef2acc79c0e4fa312431d13f7025391b787aa2b652eac55093ef

Changed files

  • src/utilities/get-url/index.ts (modified, +6/-11)

Code Example

const getServerSideURL = () => {
  let url = process.env.NEXT_PUBLIC_SERVER_URL
  url = 'http://localhost:3000'

  return url
}

---

children: [
    "Get ",
    (process.env.NEXT_PUBLIC_SERVER_URL,
    (process.env.NEXT_PUBLIC_SERVER_URL =
      "http://localhost:3000"),
    process.env.NEXT_PUBLIC_SERVER_URL),
    " started by editing ",
    (0, d.jsx)("code", { children: "app/page.tsx" }),
    ".",
  ]

---

children: [
    "Get ",
    ("http://localhost:3000",
    ("http://localhost:3000" =
      "http://localhost:3000"),
    "http://localhost:3000"),
    " started by editing ",
    (0, d.jsx)("code", { children: "app/page.tsx" }),
    ".",
  ]

---

"http://localhost:3000" =
      "http://localhost:3000"

---

alessio@Alessio-Gravili's-MacBook-Pro-(16-inch,-2024)-M4-Max cissue % pnpm next build --experimental-build-mode generate
Next.js 15.5.3
   - Environments: .env

   Creating an optimized production build ...
Collecting page data    
   Inlining static env ...
Error occurred prerendering page "/". Read more: https://nextjs.org/docs/messages/prerender-error
SyntaxError: Invalid left-hand side in assignment
    at <unknown> (.next/server/app/page.js:1169)
Export encountered an error on /page: /, exiting the build.
  Next.js build worker exited with code: 1 and signal: null

---

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.6.0: Mon Aug 11 21:15:09 PDT 2025; root:xnu-11417.140.69.701.11~1/RELEASE_ARM64_T6041
  Available memory (MB): 49152
  Available CPU cores: 16
Binaries:
  Node: 24.0.1
  npm: 10.9.0
  Yarn: N/A
  pnpm: 10.11.0
Relevant Packages:
  next: 15.5.3 // Latest available version is detected (15.5.3).
  eslint-config-next: 15.5.3
  react: 19.1.0
  react-dom: 19.1.0
  typescript: 5.9.2
Next.js Config:
  output: N/A
RAW_BUFFERClick to expand / collapse

Link to the code that reproduces this issue

https://github.com/AlessioGr/cissue

To Reproduce

  1. Download my reproduction (or create a new app using create-next-app and apply this commit)
  2. Create an .env file and add NEXT_PUBLIC_SERVER_URL='http://localhost:3000'
  3. Run pnpm next build --experimental-build-mode compile. This will generate the invalid code
  4. Run pnpm next build --experimental-build-mode generate - this will inline the environment variables and cause the invalid code to error

Important to note: running pnpm next build works fine.

The issue is the following function (used in a React component to make sure it gets executed during generate step):

const getServerSideURL = () => {
  let url = process.env.NEXT_PUBLIC_SERVER_URL
  url = 'http://localhost:3000'

  return url
}

Result after pnpm next build --experimental-build-mode compile:

children: [
    "Get ",
    (process.env.NEXT_PUBLIC_SERVER_URL,
    (process.env.NEXT_PUBLIC_SERVER_URL =
      "http://localhost:3000"),
    process.env.NEXT_PUBLIC_SERVER_URL),
    " started by editing ",
    (0, d.jsx)("code", { children: "app/page.tsx" }),
    ".",
  ]

Result after pnpm next build --experimental-build-mode generate:

children: [
    "Get ",
    ("http://localhost:3000",
    ("http://localhost:3000" =
      "http://localhost:3000"),
    "http://localhost:3000"),
    " started by editing ",
    (0, d.jsx)("code", { children: "app/page.tsx" }),
    ".",
  ]

Invalid code:

 "http://localhost:3000" =
      "http://localhost:3000"

=> error

alessio@Alessio-Gravili's-MacBook-Pro-(16-inch,-2024)-M4-Max cissue % pnpm next build --experimental-build-mode generate
   ▲ Next.js 15.5.3
   - Environments: .env

   Creating an optimized production build ...
 ✓ Collecting page data    
   Inlining static env ...
Error occurred prerendering page "/". Read more: https://nextjs.org/docs/messages/prerender-error
SyntaxError: Invalid left-hand side in assignment
    at <unknown> (.next/server/app/page.js:1169)
Export encountered an error on /page: /, exiting the build.
 ⨯ Next.js build worker exited with code: 1 and signal: null

Current vs. Expected behavior

Current: invalid code is generated, leading to error when executed Correct: no invalid code, no error

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.6.0: Mon Aug 11 21:15:09 PDT 2025; root:xnu-11417.140.69.701.11~1/RELEASE_ARM64_T6041
  Available memory (MB): 49152
  Available CPU cores: 16
Binaries:
  Node: 24.0.1
  npm: 10.9.0
  Yarn: N/A
  pnpm: 10.11.0
Relevant Packages:
  next: 15.5.3 // Latest available version is detected (15.5.3).
  eslint-config-next: 15.5.3
  react: 19.1.0
  react-dom: 19.1.0
  typescript: 5.9.2
Next.js Config:
  output: N/A

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

SWC, Output

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

next build (local)

Additional context

No response

extent analysis

TL;DR

The issue can be fixed by modifying the getServerSideURL function to avoid reassigning a string literal, which is not a valid left-hand side in an assignment.

Guidance

  • The error occurs because the getServerSideURL function is being inlined and optimized, resulting in an invalid assignment operation.
  • To fix this, modify the getServerSideURL function to avoid reassigning the url variable, for example by using a conditional statement to set the url variable only once.
  • Verify that the fix works by running pnpm next build --experimental-build-mode generate and checking that the build completes without errors.
  • Consider refactoring the getServerSideURL function to make it more robust and less prone to optimization-related issues.

Example

const getServerSideURL = () => {
  let url = process.env.NEXT_PUBLIC_SERVER_URL;
  if (!url) {
    url = 'http://localhost:3000';
  }
  return url;
}

Notes

  • This fix assumes that the intention of the getServerSideURL function is to return a default URL if the NEXT_PUBLIC_SERVER_URL environment variable is not set.
  • If the intention is different, the fix may need to be adjusted accordingly.

Recommendation

Apply workaround: modify the getServerSideURL function to avoid reassigning a string literal, as shown in the example above. This should fix the error and allow the build to complete successfully.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

nextjs - ✅(Solved) Fix experimental-build-mode compile outputs invalid code [5 pull requests, 1 comments, 2 participants]