Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

TypeScript cannot find the module @tanstack/react-query

ErrorCannot find module '@tanstack/react-query' or its corresponding type declarations.

Short answer

Either the package is not installed under this exact name, or the editor cannot see it. Install @tanstack/react-query with your package manager. If you use Yarn Plug'n'Play, the editor's TypeScript needs the Yarn SDK, so the command-line build passes while VS Code still shows the error.

  • Applies to TanStack Query 5, TypeScript 5.9, Yarn 4, VS Code
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Check whether the package is in package.json and whether tsc itself reports the error, or only the editor does.

    grep -n "react-query" package.json
    npx tsc --noEmit
  2. 2Match what you see, then jump to its fix

    1. 1tsc reports the same error and package.json has no @tanstack/react-queryThe package is not installed under this nameFix
    2. 2yarn build and tsc pass, but VS Code shows the red errorYarn Plug'n'Play: the editor does not load the PnP loaderFix

What does the error look like?

  • Cannot find module '@tanstack/react-query' or its corresponding type declarations.

Cause 1 of 2

The package is not installed under this name

1Check

The install page names the package @tanstack/react-query. If it is missing from package.json, or listed under a different name, TypeScript has nothing to resolve.

grep -n "react-query" package.json

2Fix

Install the package with the manager your project uses, then reinstall dependencies.

npm i @tanstack/react-query
# or: pnpm add @tanstack/react-query
# or: yarn add @tanstack/react-query

3Verify

Compile the project again.

npx tsc --noEmit

Confirmed by tanstack.com

Did this fix it?

Cause 2 of 2

Yarn Plug'n'Play: the editor does not load the PnP loader

1Check

Yarn PnP works by generating a Node.js loader, and many editor tools run packages without using it, so they cannot resolve modules.

ls .pnp.cjs

2Fix

Generate the editor SDK, install the ZipFS extension, and choose the workspace TypeScript version in VS Code. The SDK command reads the root package.json, so typescript must be listed there.

yarn dlx @yarnpkg/sdks vscode
# then in VS Code: ctrl+shift+p > Select TypeScript Version > Use Workspace Version

3Verify

Reopen the file; the error is gone and imports from @tanstack/react-query resolve.

yarn tsc --noEmit

Confirmed by yarnpkg.com

Did this fix it?

How do I stop it coming back?

  • Add typescript to the root package.json of every Yarn PnP repo.
  • Commit the generated .yarn/sdks and .vscode/settings.json files so the team shares the editor setup.
  • Keep TypeScript on a version that TanStack Query supports; its docs name the minimum.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

TypeScript rejects MUI Data Grid columns that set a type

A TypeScript error inside a column's renderCell callback

A columns array declared without a type is inferred with type as a plain string, which the Data Grid's column type union does not accept.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.