Terminal showing React Query install commands for npm, Yarn, pnpm and Bun

React Query npm installation takes a single command, but a few details decide whether the setup works smoothly: the correct package name, a compatible React version and the provider that connects your components to the cache. This article covers every common package manager and the checks that confirm a clean install.

The modern package is published as @tanstack/react-query. The older react-query package name belongs to version 3 and should only appear in legacy projects that have not migrated yet.

Checking the Requirements

TanStack Query v5 needs React 18 or later and a current Node.js release for your tooling. TypeScript users should run a reasonably recent TypeScript version. Confirm your React version before installing so peer dependency warnings do not surprise you.

Terminal
node --version
npm list react react-dom

Installing With Each Package Manager

Run the command that matches the lockfile already in your project. Mixing package managers creates conflicting lockfiles and inconsistent dependency trees, so stick with the one your team uses.

Package managerCommandLockfile
npmnpm install @tanstack/react-querypackage-lock.json
Yarnyarn add @tanstack/react-queryyarn.lock
pnpmpnpm add @tanstack/react-querypnpm-lock.yaml
Bunbun add @tanstack/react-querybun.lock or bun.lockb

Installing the Optional Packages

The core package is all you need to start. Add the DevTools for development insight and the ESLint plugin to catch common mistakes such as missing query key dependencies.

Terminal
npm install @tanstack/react-query-devtools
npm install -D @tanstack/eslint-plugin-query

Connecting the Provider

Installation alone does nothing until the app has a QueryClient. Create it once at module level and wrap your root component. Hooks used outside this provider throw the No QueryClient set error, which is the most frequent setup issue.

main.jsx
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';

const queryClient = new QueryClient();

root.render(
  <QueryClientProvider client={queryClient}>
    <App />
  </QueryClientProvider>
);

Setup in Next.js

With the App Router, providers must run on the client. Create a small providers component marked with the use client directive, create the QueryClient inside useState so each browser session keeps one stable instance, and wrap your layout children with it.

Verifying the Installation

A quick check prevents confusing errors later. Confirm the installed version, then render a tiny query to prove the provider works.

Terminal
npm list @tanstack/react-query
npm view @tanstack/react-query version

The first command shows the version in your project, and the second shows the latest version published on npm. If they differ by a major number, read the v5 changes before upgrading.

Fixing Common Installation Errors

Most install problems come from version mismatches or duplicated packages. The list below covers the messages developers see most often and the usual fix.

  • Peer dependency warnings about React usually mean the project still runs React 17.
  • Two copies of the library in node_modules can break context; deduplicate them.
  • Importing from react-query instead of @tanstack/react-query mixes major versions.
  • Stale lockfiles after switching managers should be removed and regenerated.

Clearing a Broken Install

When errors persist, delete node_modules and the lockfile, then reinstall with a single package manager. This rebuilds the dependency tree from scratch and resolves most duplicate package issues.

Terminal
rm -rf node_modules package-lock.json
npm install

Installing in a Monorepo

Workspaces managed by pnpm, Yarn or npm should install React Query in the package that renders React, not only at the root. Keep a single version across apps to avoid multiple React contexts when packages share components.

Updating to a Newer Version

Minor and patch updates are usually safe within the same major version. Run your package manager's update command and review release notes on GitHub. Major upgrades deserve a dedicated branch, the official codemod where available and a full test run.

Terminal
npm install @tanstack/react-query@latest
npm install @tanstack/react-query-devtools@latest

Update the core and DevTools packages together so their versions stay aligned. The GitHub article explains where release notes and source code live.

Installing With TypeScript

The package ships its own type definitions, so no separate types package is needed. After installation, hooks infer the data type from your query function automatically. Typing the return value of each fetcher gives every component accurate types without manual generics.

Strict mode in TypeScript works well with the library and catches mistakes such as reading data before checking the pending state. If your editor shows missing types, restart the TypeScript server after installing, since some editors cache module resolution.

api.ts
export async function fetchUser(id: number): Promise<User> {
  const res = await fetch(`/api/users/${id}`);
  if (!res.ok) throw new Error('User not found');
  return res.json();
}

Choosing Between npm, Yarn, pnpm and Bun

Every manager installs the same published package, so the choice depends on your team rather than React Query. npm comes with Node.js and needs no extra setup. Yarn and pnpm offer fast installs and strong workspace support, while Bun combines a runtime and package manager in one tool.

Continuous integration should use the same manager and lockfile as local development. A clean install command such as npm ci or pnpm install with a frozen lockfile guarantees that every build uses identical dependency versions.

Configuring the ESLint Plugin

After installing the ESLint plugin, add its recommended configuration to your lint setup. The rules flag query functions that use variables missing from the key and other patterns that commonly cause stale or mixed-up data.

Keep the QueryClient in its own module so tests and providers can import the same configuration. Place fetch functions in an api folder and wrap each query in a custom hook inside the feature that uses it. This layout keeps components small and makes keys easy to find.

Global defaults such as staleTime and retry belong in the QueryClient options rather than in individual hooks. Individual queries then override only what differs, which keeps behavior consistent across the application and simplifies future upgrades.

Sharing Configuration Across Apps

Shared configuration also helps when several apps live in one repository. Exporting a function that creates a configured QueryClient keeps defaults identical across apps while still giving each app and each test its own isolated cache.

Confirming Everything Works

Render a component that runs one small query and open the DevTools. Seeing the key appear, turn fresh and later stale confirms that the package, provider and configuration are all connected correctly.

Conclusion

React Query npm setup comes down to three moves: install @tanstack/react-query with your existing package manager, create a single QueryClient and wrap the app in QueryClientProvider.

Once the provider is in place, follow the React Query tutorial to write your first query, and add the DevTools to watch the cache from day one.