next.js: Attempted import error: 'useRef' is not exported from 'react' (imported as 'React')
Link to the code that reproduces this issue
https://github.com/cthacker-udel/BugRepros
To Reproduce
My layout.tsx:
/* eslint-disable react/hook-use-state -- disabled */
"use client";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
import React, { type ReactNode } from "react";
/**
* The properties of the QueryProvider component
*/
type QueryProviderProperties = {
/**
* The element being "wrapped" by the provider
*/
readonly children: ReactNode;
};
/**
* The provider for the Query implementation
*
* @param children - The element being "wrapped" by the provider
* @returns The provider with the passed down `queryClient` value
*/
export const QueryProvider = ({
children,
}: QueryProviderProperties): JSX.Element => {
const [queryClient] = React.useState(
() =>
new QueryClient({
defaultOptions: {
queries: {
staleTime: 60 * 1000,
},
},
}),
);
return (
<QueryClientProvider client={queryClient}>
{children}
<ReactQueryDevtools initialIsOpen={false} />
</QueryClientProvider>
);
};
Result:
import error: 'useRef' is not exported from 'react' (imported as 'React').
Import trace for requested module:
./node_modules/@tanstack/react-query-devtools/build/modern/devtools.js
./node_modules/@tanstack/react-query-devtools/build/modern/index.js
./src/providers/query/QueryProvider.tsx
Current vs. Expected behavior
Current: Crashing using next dev
, and crashing during next build
, completely blocking development.
Expected: Working as it was before updating next to latest
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 Home
Binaries:
Node: 20.9.0
npm: N/A
Yarn: N/A
pnpm: N/A
Which area(s) are affected? (Select all that apply)
Module resolution (CJS / ESM, module resolving)
Which stage(s) are affected? (Select all that apply)
next dev (local), next build (local), Vercel (Deployed)
Additional context
The bug is in the tanstackbug
folder of the repo
About this issue
- Original URL
- State: closed
- Created 5 months ago
- Reactions: 16
- Comments: 30 (3 by maintainers)
Commits related to this issue
- build(npm): add `--turbo` flag to dev workaround to issue introduced in next.js 14.1.0 (https://github.com/vercel/next.js/issues/60890) that only seems to affect dev builds for me — committed to CU-CommunityApps/ct-azure-openai-gpt4-chat by mikesprague 5 months ago
- Fix attempted import error for react (#61791) ### What Exclude precompiled react packages from browser layer loaders coverage. ### Why Since we're transpiling all the browser layer code now ... — committed to vercel/next.js by huozhi 5 months ago
- Configure fixes for vercel/next.js#60890. - Convert function declarations to fat arrows. - Use experimental Turbo builder in development. — committed to phyllisstein/ar-technical-test by phyllisstein 5 months ago
- fix: random next error on playground https://github.com/vercel/next.js/issues/60890 — committed to chakra-ui/panda by astahmer 4 months ago
- chore: update ts-morph to v21.0.1 to match TS 5.3 (#2221) * chore: update ts-morph to v21.0.1 to match TS 5.3 https://github.com/dsherret/ts-morph/blob/latest/packages/ts-morph/CHANGELOG.md#2100-202... — committed to chakra-ui/panda by astahmer 4 months ago
- Fix attempted import error for react (#61791) ### What Exclude precompiled react packages from browser layer loaders coverage. ### Why Since we're transpiling all the browser layer code now ... — committed to vercel/next.js by huozhi 5 months ago
Same issue here with
react-query
.In addition I am getting this error, which is maybe related:
Interestingly, the problems are gone when I use
next dev --turbo
.Solved this problem in next js 14.1.0, solution: I was able to solve this problem by removing browserslist:[…] of the package.json and I admit that this version is much more optimal. thanks
Happening to me as well with several imports, all from React (
createElement
,useState
,flushSync
, etc) since updating to 14.1 this morning. Worked ok with 14.0.4.We had a fix in #61791 and released in v14.1.1-canary.44 for this issue 🙏
Adding this to package.json finally solved this issue. Deleted node_modules/.cache and .next folders. During the first run there where many errors but after that the app runs again.
Hope the root cause will be fixed soon to remove this workaround again.
@NanningR last time I tried turbo it wasn’t quite working for me, I’ll try again thanks.
@mikesprague fwiw this does affect production for me as well, the build step fails with the same error.
Thanks! It worked, but only after I also deleted the .next folder.
I’m having the same issue. The error occurs for the first time in 14.0.5-canary.20
I had this too - I didn’t have browserslist but it popped up when I used the shad-cn dropdown-ui component.
I had to add
"use client"
to to component (which makes sense) but then I had to nuke my.next
folder for it to actually make the error go away.@cthacker-udel I’m not sure what’s going on with next dev behind the scenes either, but I’m glad this temporary solution worked for you.
@alessandrojcm Another temporary solution that might work for you is changing your package.json, such that it runs “next dev” with the experimental/beta turbo flag:
"scripts": { "dev": "next dev --turbo",
This works for me, even without the browserlist tweak. However, I need to run without the turbo flag because it’s still in beta, and it breaks the functionality of some of my pages. It might help you though
+1 . Build is breaking showing the following error “Attempted import error: ‘useRef’ is not exported from ‘react’ (imported as ‘e’).” after upgrading from 14.0.4. I’ve tried removing
browserslist
in package.json and does not work.Going off https://github.com/vercel/next.js/issues/60909, we’ve worked around this by adding
Chrome 118
explicitly in our.browserslistrc
file so it looks like the following:It’s very random, I originally had an issue with an import from the
@mantine/tiptap
package. Thought that maybe I was importing it from a server component or something but after I removed that package the issue just came from another import.I thought the issue for me was that I was accidentally importing something into middleware that wasn’t being tree shaken properly, but I guess it’s not based on other comments?
Don’t have browserlists in package.json so I cannot delete that. For me it doesn’t crash the build process, but just pollutes my terminal during development or build and I’m also not sure if something might break during runtime later when app is running.
It also doesn’t list React imports as problematic, but rather a react-hook-form export. I get this message multiple times: