vercel / next.js

The React Framework
https://nextjs.org
MIT License
122.51k stars 26.22k forks source link

Turbopack imports a json file instead of module #66835

Open SpeedyCoder opened 1 week ago

SpeedyCoder commented 1 week ago

Link to the code that reproduces this issue

https://github.com/wontondon/hypertune-nextjs-turbo-error

To Reproduce

  1. Clone the repo
  2. Run npm install
  3. Run npm run devt

Current vs. Expected behavior

Current

When loading the root page in dev mode using Turbopack we get the following error.

 ⨯ TypeError: Class extends value undefined is not a constructor or null

This might be caused by a React Class Component being rendered in a Server Component, React Class Components only works in Client Components. Read more: https://nextjs.org/docs/messages/class-component-in-server-component
    at <unknown> (./generated/hypertune.ts:92:35)
    at [project]/generated/hypertune.ts [app-rsc] (ecmascript) (/Users/hypertune/projects/hypertune-nextjs-turbo-error/.next/server/chunks/ssr/_cefa7c._.js:285:3)
    at instantiateModule (./[turbopack]/build/runtime.ts:192:19)
    at getOrInstantiateModuleFromParent (./[turbopack]/build/runtime.ts:252:10)
    at esmImport (./[turbopack]/shared/runtime-utils.ts:215:18)
    at <unknown> (./src/lib/getHypertune.ts:0:0)
    at [project]/src/lib/getHypertune.ts [app-rsc] (ecmascript) (/Users/hypertune/projects/hypertune-nextjs-turbo-error/.next/server/chunks/ssr/_cefa7c._.js:319:3)
    at instantiateModule (./[turbopack]/build/runtime.ts:192:19)
    at getOrInstantiateModuleFromParent (./[turbopack]/build/runtime.ts:252:10)
    at esmImport (./[turbopack]/shared/runtime-utils.ts:215:18)
    at <unknown> (./src/app/page.tsx:0:0)
    at [project]/src/app/page.tsx [app-rsc] (ecmascript) (/Users/hypertune/projects/hypertune-nextjs-turbo-error/.next/server/chunks/ssr/_cefa7c._.js:348:3)
    at instantiateModule (./[turbopack]/build/runtime.ts:192:19)
    at getOrInstantiateModuleFromParent (./[turbopack]/build/runtime.ts:252:10)
    at esmImport (./[turbopack]/shared/runtime-utils.ts:215:18)
    at Module.default (/Users/hypertune/projects/hypertune-nextjs-turbo-error/.next/server/chunks/ssr/_cefa7c._.js:352:20)
    at Array.map (<anonymous>)
digest: "2193850115"
  90 | const rootFallback = {showStripeSubscriptionButtons:false,showPlanManagement:false,showOrgSignup:false};
  91 |
> 92 | export class RootNode extends sdk.Node {
     |                                   ^
  93 |   typeName = "Root" as const;
  94 |
  95 |   getRootArgs(): RootArgs {

This is caused by Turbopack loading the hypertune.json file from the root of the repository instead of the hypertune module. I've confirmed this by inspecting the loaded module in the generated/hypertune.ts immediately following the import statement. Removing or renaming the json file also fixes the issue.

Expected

The correct module is loaded and the page loads without issues even with the json file present in the root of the repository. This is the case when using next dev without the turbo flag, what suggests Turbopack is causing the problem.

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.4.0: Fri Mar 15 00:12:49 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T6020
  Available memory (MB): 32768
  Available CPU cores: 12
Binaries:
  Node: 18.18.0
  npm: 9.8.1
  Yarn: 1.22.22
  pnpm: 9.1.4
Relevant Packages:
  next: 14.2.4 // Latest available version is detected (14.2.4).
  eslint-config-next: 13.5.1
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.4.2
Next.js Config:
  output: N/A

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

Developer Experience, Turbopack

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

next dev (local)

Additional context

I've tried updating all the dependencies to the latest versions, but that didn't fix the issue.

timneutkens commented 1 week ago

Looks like this is related to "baseUrl": ".", in tsconfig.json. Looks like it will resolve .json whereas in webpack we it doesn't resolve json files.

Changing the .json to .js or .ts reproduces the resolving with tsc but not with webpack. Seems there's a bug in the webpack handling that doesn't like up as well