next.js: client side pages not working - next13

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

    Operating System:
      Platform: win32
      Arch: x64
      Version: Windows 10 Pro   
    Binaries:
      Node: 18.12.0
      npm: N/A
      Yarn: N/A
      pnpm: N/A
    Relevant packages:
      next: 13.0.1-canary.4     
      eslint-config-next: 13.0.0
      react: 18.2.0
      react-dom: 18.2.0

What browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

Describe the Bug

Unhandled Runtime Error
Error: Cannot read properties of undefined (reading 'default')

Call Stack
resolveModuleMetaData
webpack-internal:///(sc_server)/./node_modules/next/dist/compiled/react-server-dom-webpack/server.browser.js (195:82)
serializeModuleReference
webpack-internal:///(sc_server)/./node_modules/next/dist/compiled/react-server-dom-webpack/server.browser.js (1298:50)
resolveModelToJSON
webpack-internal:///(sc_server)/./node_modules/next/dist/compiled/react-server-dom-webpack/server.browser.js (1660:40)
Array.toJSON
webpack-internal:///(sc_server)/./node_modules/next/dist/compiled/react-server-dom-webpack/server.browser.js (1081:40)
stringify
<anonymous>
processModelChunk
webpack-internal:///(sc_server)/./node_modules/next/dist/compiled/react-server-dom-webpack/server.browser.js (163:36)
retryTask
webpack-internal:///(sc_server)/./node_modules/next/dist/compiled/react-server-dom-webpack/server.browser.js (1823:50)
performWork
webpack-internal:///(sc_server)/./node_modules/next/dist/compiled/react-server-dom-webpack/server.browser.js (1856:33)
eval
webpack-internal:///(sc_server)/./node_modules/next/dist/compiled/react-server-dom-webpack/server.browser.js (1257:40)
scheduleWork
webpack-internal:///(sc_server)/./node_modules/next/dist/compiled/react-server-dom-webpack/server.browser.js (52:25)

Expected Behavior

expected page to be rendered without error on client side, using the example

https://beta.nextjs.org/docs/data-fetching/fetching#example-fetch-and-use-in-client-components

"use client";

import { use } from 'react';

async function getData() {
  const res = await fetch('https://api.example.com/...');
  // The return value is *not* serialized
  // You can return Date, Map, Set, etc.
  return res.json();
}

export default function Page() {
  const name = use(getData());

  return '...';
}

Link to reproduction

no link

To Reproduce

// ~/app/categories/page.tsx

"use client";

import { use } from 'react'
import { getCategories } from '../../lib/utils'

export default function Page() {

    let categories= use(getCategories())
    console.log(categories)

    return ...
}
// ~/lib/utils.tsx

export async function getCategories() {
    let res = await fetch("https://....");
    return res.json();
}

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 8
  • Comments: 22 (5 by maintainers)

Most upvoted comments

[Not working only in Windows] Tested using create-next-app@13.3.0, 13.1.1, and 13.2.5-canary.25. When I add “use client” at the top of page.tsx, I get the following error:

error - SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous>)

I get the same error as well

error - SyntaxError: Unexpected token u in JSON at position 0
at JSON.parse ()

This is my client component

"use client"

import { useState } from "react"

export default function UserSearch() {
  const [searchTerm, setSearchTerm] = useState("")

  return (
    <div className="w-full bg-white border border-gray-200 rounded-lg shadow dark:bg-gray-800 dark:border-gray-700">
      <div className="flex flex-col items-center py-10">
        <h5 className="mb-1 text-xl font-medium text-gray-900 dark:text-white">Search</h5>
        <input
          className="w-1/2 px-4 py-2 mt-2 text-gray-700 bg-white border border-gray-300 rounded-lg shadow-sm focus:border-blue-500 focus:ring focus:ring-blue-200 focus:ring-opacity-50"
          type="text"
          placeholder="Search for users"
          value={searchTerm}
          onChange={e => setSearchTerm(e.target.value)}
        />
      </div>
    </div>
  )
}

and I import the component in my page.tsx

Changing the component to an arrow function works. Example:

function UserSearch(){ //… return … } To

Const UserSearch= () => { //… return … }

I still have this error, even if I add the "use client" at the top of the document, I see there is a log in the server side

I get the same error as well

error - SyntaxError: Unexpected token u in JSON at position 0
at JSON.parse ()

This is my client component

"use client"

import { useState } from "react"

export default function UserSearch() {
  const [searchTerm, setSearchTerm] = useState("")

  return (
    <div className="w-full bg-white border border-gray-200 rounded-lg shadow dark:bg-gray-800 dark:border-gray-700">
      <div className="flex flex-col items-center py-10">
        <h5 className="mb-1 text-xl font-medium text-gray-900 dark:text-white">Search</h5>
        <input
          className="w-1/2 px-4 py-2 mt-2 text-gray-700 bg-white border border-gray-300 rounded-lg shadow-sm focus:border-blue-500 focus:ring focus:ring-blue-200 focus:ring-opacity-50"
          type="text"
          placeholder="Search for users"
          value={searchTerm}
          onChange={e => setSearchTerm(e.target.value)}
        />
      </div>
    </div>
  )
}

and I import the component in my page.tsx

Same compilation error here (using appDir) Both static and dynamic imports are triggering the server to try to pre-render it. It obviously does not work as I’m using window in a Client Component.

[Not working only in Windows] Tested using create-next-app@13.3.0, 13.1.1, and 13.2.5-canary.25. When I add “use client” at the top of page.tsx, I get the following error:

error - SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse ()

I also had a similar error using version 13.3.0 but when I used version 13.1.1 the error disappeared make sure that your NextJS version matches the version that you specified when you ran npx create-next-app@13.1.1 --experimental-app although I specified installing version 13.1.1 in the command, it installed version 13.3.0 when I looked at my package.json file. so, I manually changed the version in my package.json file, deleted the node_modules folder, and installed all the packages again. and then the error was fixed

I get the same error as well

error - SyntaxError: Unexpected token u in JSON at position 0
at JSON.parse ()

This is my client component

"use client"

import { useState } from "react"

export default function UserSearch() {
  const [searchTerm, setSearchTerm] = useState("")

  return (
    <div className="w-full bg-white border border-gray-200 rounded-lg shadow dark:bg-gray-800 dark:border-gray-700">
      <div className="flex flex-col items-center py-10">
        <h5 className="mb-1 text-xl font-medium text-gray-900 dark:text-white">Search</h5>
        <input
          className="w-1/2 px-4 py-2 mt-2 text-gray-700 bg-white border border-gray-300 rounded-lg shadow-sm focus:border-blue-500 focus:ring focus:ring-blue-200 focus:ring-opacity-50"
          type="text"
          placeholder="Search for users"
          value={searchTerm}
          onChange={e => setSearchTerm(e.target.value)}
        />
      </div>
    </div>
  )
}

and I import the component in my page.tsx

Changing the component to an arrow function works. Example:

function UserSearch(){ //… return … } To

Const UserSearch= () => { //… return … }

Why it works?

13.1.1

This does works. I didn’t understand why all the time I was using 'use client" my app broke. But this fixes it.

{ssr: false} has no impact for me either (in appDir). It is always pre-rendered.