vercel / next.js

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

[regression] "'useContext' is not exported from 'react'" error message in latest canary, works fine in v14.1.0 #62187

Closed stefanprobst closed 8 months ago

stefanprobst commented 9 months ago

Link to the code that reproduces this issue

https://github.com/stefanprobst/issue-next-browserslist

To Reproduce

  1. clone repo, pnpm install
  2. pnpm run build
  3. see compilation error message:
./node_modules/.pnpm/next@14.1.1-canary.58_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/esm/client/components/navigation.js
Attempted import error: 'useContext' is not exported from 'react' (imported as 'useContext').

Import trace for requested module:
./node_modules/.pnpm/next@14.1.1-canary.58_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/esm/client/components/navigation.js
./node_modules/.pnpm/next@14.1.1-canary.58_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/esm/api/navigation.js
./node_modules/.pnpm/next-intl@3.7.0_next@14.1.1-canary.58_react@18.2.0/node_modules/next-intl/dist/esm/server/react-server/RequestLocale.js
./node_modules/.pnpm/next-intl@3.7.0_next@14.1.1-canary.58_react@18.2.0/node_modules/next-intl/dist/esm/server.react-server.js
./app/[locale]/opengraph-image.tsx
./node_modules/.pnpm/next@14.1.1-canary.58_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/build/webpack/loaders/next-metadata-route-loader.js?page=%2F%5Blocale%5D%2Fopengraph-image%2Froute&isDynamic=1!./app/[locale]/opengraph-image.tsx?__next_metadata_route__
./node_modules/.pnpm/next@14.1.1-canary.58_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/build/webpack/loaders/next-app-loader.js?name=app%2F%5Blocale%5D%2Fopengraph-image%2Froute&page=%2F%5Blocale%5D%2Fopengraph-image%2Froute&pagePath=private-next-app-dir%2F%5Blocale%5D%2Fopengraph-image.tsx&appDir=%2Fdata%2FDevelopment%2Fplayground%2F__tmp%2Fissue-next-browserslist%2Fapp&appPaths=%2F%5Blocale%5D%2Fopengraph-image&pageExtensions=tsx&pageExtensions=ts&pageExtensions=jsx&pageExtensions=js&basePath=&assetPrefix=&nextConfigOutput=&preferredRegion=&middlewareConfig=e30%3D!./app/[locale]/opengraph-image.tsx?__next_edge_ssr_entry__

Current vs. Expected behavior

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #38~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Thu Nov  2 18:01:13 UTC 2
Binaries:
  Node: 20.11.1
  npm: 10.2.4
  Yarn: 1.22.19
  pnpm: 8.15.3
Relevant Packages:
  next: 14.1.1-canary.58 // Latest available version is detected (14.1.1-canary.58).
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.3.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

No response

NEXT-2519

huozhi commented 9 months ago

You're using notFound API from next/navigation in i18n.ts, seems like that file is also going to be used for og image. But og image are basically the app route where we're using react-server condition, and should return a 404 response for responding "not found" instead of call notFound() API. If you comment out that API it works well.

stefanprobst commented 9 months ago

thanks for investigating! to clarify, v14.1.0 not showing that error message was actually incorrect? (also, why the difference between "edge" and "node" runtime?)

stefanprobst commented 9 months ago

also not sure what you are suggesting to comment out -- when removing notFound() (like here) is still see the same error message.

steven-tey commented 8 months ago

Running into the same error as well – looks like it's an issue with the edge runtime + useContext hook

Not blocking any builds, but it keeps showing up in the build logs + terminal for every HMR 😅

bluebeel commented 8 months ago

Same error for us. If we have a page with edge runtime, we get the following error:

../../node_modules/next/dist/esm/client/components/navigation.js
Attempted import error: 'useContext' is not exported from 'react' (imported as 'useContext').

Import trace for requested module:
../../node_modules/next/dist/esm/client/components/navigation.js
../../node_modules/next/dist/esm/api/navigation.js
../../node_modules/next-intl/dist/esm/server/react-server/RequestLocale.js
../../node_modules/next-intl/dist/esm/server.react-server.js
./app/[locale]/layout.tsx
../../node_modules/next/dist/build/webpack/loaders/next-app-loader.js?name=app%2F%5Blocale%5D%2Ftransactions%2Fdeposits%2Fpage&page=%2F%5Blocale%5D%2Ftransactions%2Fdeposits%2Fpage&pagePath=private-next-app-dir%2F%5Blocale%5D%2Ftransactions%2Fdeposits%2Fpage.tsx&appDir=%2FUsers%2Fsaikoubarry%2FDocuments%2FGreenIsland%2Fstarcasino-portal-client%2Fapps%2Fstarcasinodice%2Fapp&appPaths=%2F%5Blocale%5D%2Ftransactions%2Fdeposits%2Fpage&pageExtensions=tsx&pageExtensions=ts&pageExtensions=jsx&pageExtensions=js&basePath=&assetPrefix=&nextConfigOutput=&preferredRegion=&middlewareConfig=e30%3D!./app/[locale]/transactions/deposits/page.tsx?__next_edge_ssr_entry__

In the page we doesn't use the notFound() api

import { Metadata } from 'next';
import { AppRoutes } from '@org/constants';
import { ComponentA } from './_components/ComponentA';
import { getSession, withPageAuthRequired } from '@auth0/nextjs-auth0/edge';

export const runtime = 'edge';

export default withPageAuthRequired(
  async function Page({
    searchParams,
  }: {
    searchParams: { [key: string]: string | string[] | undefined };
  }) {
    const session = await getSession();

    const queries = {};

    return <ComponentA {...queries} userId={session.user.userId} />;
  },
  {
    returnTo({ params }) {
      return `${params.locale === 'en' ? '' : `/${params.locale}`}/${AppRoutes.PageA}`;
    },
  }
);
huozhi commented 8 months ago

Yea looks like previously we didn't alias the correct react bundle, and after we got the correct react bundle on server components and app routes side, then we can analysis the correct exports usage of react. next/navigation needs to be separated into 2 parts, the one used only on server side shouldn't need to touch the imports of React. Got a PR to fix it

github-actions[bot] commented 8 months ago

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.