next.js: [NEXT-940] Unexpected token u in JSON because of "use client"
Verify canary release
- I verified that the issue exists in the latest Next.js canary release
Provide environment information
Operating System:
Platform: win64
Arch: x64
Version: Windows 11 Home
Binaries:
Node: 18.15.0
npm: N/A
Yarn: N/A
pnpm: N/A
Relevant packages:
next: 13.2.5-canary.21
eslint-config-next: 13.2.4
react: 18.2.0
react-dom: 18.2.0
Which area(s) of Next.js are affected? (leave empty if unsure)
Static HTML Export (next export)
Link to the code that reproduces this issue
https://github.com/thewbuk/nextjs13_error
To Reproduce
npm run build
Describe the Bug
When trying to build project, SyntaxError: Unexpected token u in JSON at position 0
is thrown for every page that uses "use client"
even though the latest release of the canary is supposed to fix this issue.
Using App
directory
Expected Behavior
Create static export
Which browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 38
- Comments: 46 (10 by maintainers)
Commits related to this issue
- Components to arrow functions To avoid a bug (https://github.com/vercel/next.js/issues/47704) switched all components to arrow functions (will revert once it's solved). — committed to Pau-Marin/Portfolio-Site by Pau-Marin a year ago
- Migrate project to TypeScript (#18) * Update site to v1.0.4 (#15) * Refactor cleanup (#10) * Refactor Cleaned some unused imports * Update SocialLinks.jsx Added a key for each child on... — committed to Pau-Marin/Portfolio-Site by Pau-Marin a year ago
- Fix next-flight-loader resourcePath in Windows (#48152) ### What? It's path backslash and string escape bug at [next-flight-loader](https://linear.app/vercel/issue/NEXT-flight-loader), in Windows ... — committed to vercel/next.js by motopods a year ago
- build: return to next 13.2.3 see: https://github.com/vercel/next.js/issues/47704 — committed to cantte/kyrian by CarlosPavajeau a year ago
It’s path backslash and string escape bug at next-flight-loader, in Windows https://github.com/vercel/next.js/blob/9c0e52089628c25c13a5bce6fb57b18f65dbbae1/packages/next/src/build/webpack/loaders/next-flight-loader/index.ts#L67
Possible solutions
I am not sure arrow function’s workaround is the same problem as this.
Is there anyone patch node_modules\next\dist\build\webpack\loaders\next-flight-loader\index.js, this file. then delete .next directory, restart dev. And give me some feedback?
This is not just related to pages, every component with
"use client"
causes this error in13.3.0
.Reproduction:
npx create-next-app@latest --experimental-app
app/layout.tsx
:This results in
Uncaught SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
with13.3.0
while working fine in13.2.4
.This is also affecting
next dev
, not onlynext build
, even withoutoutput: 'export'
.Confirmed here that Windows is broken.
Arrow Function Fixes It
Broken code:
The fix is to just convert to arrow function:
Platform: Windows 11 Next Version: 13.3.0 React Version: 18.2.0
Same problem in our project using 13.2.5-canary.21. It works fine on Linux but not on Windows. We’re seeing the problem with
next dev
too.For me not using the arrow function broke it.
Swapped my client component to an arrow function and it worked. Version:
13.2.5-canary.28
, Windows 11, Node: 16.14.2@leerob sorry for pinging you here, but its been almost two weeks, one stable launch. And this is still blocking all windows users from continuing using next on app dirs.
On 13.2.4 with a page that uses a client component.
When I migrate the page to a route group with a multiple root layouts I also get this error. Tested on canary as well.
On Windows.
This is just too huge of a bug to be left unoticed by the next team. So I believe we must fall under certain conditions to let this error happen.
From every person that commented here, I would like to know your operating system, node version and any other detail considered relevant.
found the issue, you’re using a fragment in your client page which throws a server mismatch in both dev and build. I got it to successfully run the build and export by changing your client page to the following:
Don’t mind the arrowfunction swap instead of a regular function, has to do with my dev setup where I autoswap react components/pages etc to arrow function.
See the repo below: https://github.com/JesseKoldewijn/nextjs13-appDir-staticExport
My next info output:
https://github.com/vercel/next.js/blob/18131c7dab79365c7025e59f6052cd81b61a9215/packages/next/src/build/analysis/get-page-static-info.ts#L45
Remove
(cjs|auto)
, and fallback next-swc.win32-x64-msvc to 13.2.5-canary.19. It works.Maybe https://github.com/vercel/next.js/blob/18131c7dab79365c7025e59f6052cd81b61a9215/packages/next-swc/crates/core/src/react_server_components.rs#L298
Thanks @shuding, really grateful for this
Confirmed, just tried and it solved it the issue. Cheers!
This error occurs when the clientEntryType is
auto
https://github.com/vercel/next.js/blob/18131c7dab79365c7025e59f6052cd81b61a9215/packages/next/src/build/analysis/get-page-static-info.ts#L65Maybe some problem with moduleProxy in windows.
Same problem here. I’ll just wait for the next stable release, as I do not think that replacing all the regular functions in my project with arrow functions is a viable option. Running Windows 11 Pro here.
I can confirm this is still not working.
Windows 11 Next 13.3
I have test on my Mac and there’s no issue, it looks looks like a Windows specific issue.
This will not work:
This will work
I am experiencing the same issue here for Versions 13.2.5-canary.20 through to 13.2.5-canary.25
13.2.5-canary.19 works fine.
@JesseKoldewijn This solution didn’t work for me. Did it work for anyone with this issue?
Windows, node 18.12.1, next 13.2.4
Edit: Just managed to solve my issue. I was exporting JSX through an async function, must have been a copy pasta.
Tried it on Node 18 and still the same error.
Confirmation of arrow functions working- Using arrow functions to all of your components where “use client” is being used fixes it. We should be using arrow functions for now until this bug gets fixed. Thanks everyone who recommended to use arrow function that actually does work
I’m using Windows - 10 and node 19.7.0
Is the team aware about this?
Yeah for me it sort of stopped breaking on build when I used arrow functions and actually having a element in the return other than a empty fragment. It did still break on a dynamic route tho cuz on the static build it only exported images used in one of the static routes and nothing else.
So it only exports the following structure:
The disfunctional dynamic route might have to do with my setup because I didn’t dive deep into the ssg usecase of it yet, so I’m not sure if you still have to generate the static paths for it.
The disfunctional image src hrefs on the index page are kinda weird to me because I’m not using anything in the homepage that scream “I’m gonna break!” to me honestly.
Might need to have a deeper look into it bur hey, atleast it partially works ish.
Repo: https://github.com/ShiftCodeEU/shiftcode.eu
^ Incase anybody knows what the deal is with the broken images or something, would appreciate it👌
Ps. Sorry for the whole essay😅
Same problem here on 13.2.5-canary.21