--- title: Next.js Guide description: How to set up Tamagui with Next.js --- Add Tamagui to a Next.js app with shared configuration, SSR-safe themes, and optional compiler extraction. Running `npm create tamagui@latest` lets you choose the configured `starter-free` Next.js app as a starting point. Create a new [Next.js](https://nextjs.org/docs/getting-started/installation) project: ```bash npx create-next-app@latest ``` We recommend starting with our default config which gives you media queries and other nice things: ```tsx fileName="tamagui.config.ts" import { defaultConfig } from '@tamagui/config/v6' import { createTamagui } from 'tamagui' // or '@tamagui/core' const appConfig = createTamagui(defaultConfig) export type AppConfig = typeof appConfig declare module 'tamagui' { // or '@tamagui/core' // overrides TamaguiCustomConfig so your custom types // work everywhere you import `tamagui` interface TamaguiCustomConfig extends AppConfig {} } export default appConfig ``` ## Setup Next.js uses Turbopack by default. In dev mode, Tamagui works without any setup. For production builds, use the Tamagui CLI to optimize your build. Install the CLI: ```bash yarn add -D @tamagui/cli ``` Create `tamagui.build.ts`: ```ts fileName="tamagui.build.ts" import type { TamaguiBuildOptions } from '@tamagui/core' export default { components: ['@tamagui/core'], // or ['tamagui'] config: './tamagui.config.ts', outputCSS: './public/tamagui.generated.css', } satisfies TamaguiBuildOptions ``` Your `next.config.ts` needs some configuration: ```ts fileName="next.config.ts" import type { NextConfig } from 'next' const nextConfig: NextConfig = { experimental: { turbo: { resolveAlias: { 'react-native': 'react-native-web', 'react-native-svg': '@tamagui/react-native-svg', }, }, }, } export default nextConfig ``` The `transpilePackages` array may need additional packages depending on which Tamagui packages you use. If you see module resolution errors, try adding the problematic package to this array. ### Build Scripts The CLI can wrap your build command, optimizing files beforehand and restoring them after: ```json fileName="package.json" { "scripts": { "dev": "next dev --turbopack", "build": "tamagui build --target web ./src -- next build" } } ``` The `--` separator tells the CLI to run `next build` after optimization, then restore your source files automatically. You can also target specific files or use `--include`/`--exclude` patterns: ```bash # Target specific files tamagui build --target web ./src/components/Button.tsx ./src/components/Card.tsx -- next build # Use glob patterns tamagui build --target web --include "src/components/**/*.tsx" --exclude "src/components/**/*.test.tsx" ./src -- next build ``` ### CSS Setup The CLI generates theme CSS to `outputCSS`. Commit this file to git and import it in your layout: ```tsx fileName="app/layout.tsx" import '../public/tamagui.generated.css' import { TamaguiProvider } from '@tamagui/core' import config from '../tamagui.config' export default function RootLayout({ children }: { children: React.ReactNode }) { return ( {children} ) } ``` With React 19, Tamagui automatically injects runtime styles via style tags. The `outputCSS` file handles themes and tokens that are generated at build time. Run `npx tamagui build` once to generate the initial CSS file, then commit it. ### CI Verification Use `--expect-optimizations` to fail builds if the compiler optimizes fewer than the expected minimum number of components: ```json { "build": "tamagui build --target web --expect-optimizations 5 ./src -- next build" } ``` This will fail the build if fewer than 5 components are optimized, helping catch configuration issues in CI. ## Themes We've created a package called `@tamagui/next-theme` that properly supports SSR light/dark themes while respecting user system preferences. It assumes your themes are named `light` and `dark`, but you can override this. This is pre-configured in the create-tamagui starter. ```bash yarn add @tamagui/next-theme ``` Here's how to set up your `NextTamaguiProvider.tsx`: ```tsx fileName="NextTamaguiProvider.tsx" 'use client' import { ReactNode } from 'react' import { NextThemeProvider, useRootTheme } from '@tamagui/next-theme' import { TamaguiProvider } from 'tamagui' import tamaguiConfig from '../tamagui.config' export const NextTamaguiProvider = ({ children }: { children: ReactNode }) => { const [theme, setTheme] = useRootTheme() return ( { setTheme(next as any) }} > {children} ) } ``` Then update your `app/layout.tsx`: ```tsx fileName="app/layout.tsx" import '../public/tamagui.generated.css' import { Metadata } from 'next' import { NextTamaguiProvider } from './NextTamaguiProvider' export const metadata: Metadata = { title: 'Your page title', description: 'Your page description', icons: '/favicon.ico', } export default function RootLayout({ children }: { children: React.ReactNode }) { return ( {children} ) } ``` ## NextThemeProvider `NextThemeProvider` lets you set the theme for your app and provides a hook to access the current theme and toggle between themes. ```bash yarn add @tamagui/next-theme ``` void', description: 'Used to change the current theme. The function receives the theme name as a parameter.', }, { name: 'systemTheme', required: false, type: 'string', description: 'System theme name for the current page.', }, { name: 'enableColorScheme', required: false, type: 'boolean', description: `Whether to indicate to browsers which color scheme is used (dark or light) for built-in UI like inputs and buttons.`, }, { name: 'disableTransitionOnChange', required: false, type: 'boolean', description: `Disable all CSS transitions when switching themes.`, }, { name: 'storageKey', required: false, type: 'string', description: `Key used to store theme setting in localStorage.`, }, { name: 'themes', required: false, type: 'string[]', description: `List of all available theme names.`, }, { name: 'value', required: false, type: 'ValueObject', description: `Mapping of theme name to HTML attribute value. Object where key is the theme name and value is the attribute value.`, }, ]} /> ### Theme toggle If you need to access the current theme, say for a toggle button, you will then use the `useThemeSetting` hook. We'll release an update in the future that makes this automatically work better with Tamagui's built-in `useThemeSetting`. ```tsx fileName="SwitchThemeButton.tsx" import { useState } from 'react' import { Button, useIsomorphicLayoutEffect } from 'tamagui' import { useThemeSetting, useRootTheme } from '@tamagui/next-theme' export const SwitchThemeButton = () => { const themeSetting = useThemeSetting() const [theme] = useRootTheme() const [clientTheme, setClientTheme] = useState('light') useIsomorphicLayoutEffect(() => { setClientTheme(themeSetting.forcedTheme || themeSetting.current || theme) }, [themeSetting.current, themeSetting.resolvedTheme]) return } ``` --- ## For Older Versions The following sections cover setup for older Next.js versions using Webpack instead of Turbopack. ### Webpack Plugin If you aren't using Turbopack, you may want the optional `@tamagui/next-plugin`, which smooths out a few settings. See the [compiler install docs](/docs/intro/compiler-install) for more options. Add `@tamagui/next-plugin` to your project: ```bash yarn add @tamagui/next-plugin ``` ## Pages Router ### next.config.js Set up the optional Tamagui plugin in `next.config.js`: ```js showMore fileName="next.config.js" const { withTamagui } = require('@tamagui/next-plugin') module.exports = function (name, { defaultConfig }) { let config = { ...defaultConfig, // ...your configuration } const tamaguiPlugin = withTamagui({ config: './tamagui.config.ts', components: ['tamagui'], }) return { ...config, ...tamaguiPlugin(config), } } ``` ### pages/\_document.tsx If you're using React Native Web components, gather the `react-native-web` styles in `_document.tsx`: ```tsx fileName="_document.tsx" import NextDocument, { DocumentContext, Head, Html, Main, NextScript, } from 'next/document' import { StyleSheet } from 'react-native' export default class Document extends NextDocument { static async getInitialProps({ renderPage }: DocumentContext) { const page = await renderPage() // @ts-ignore RN doesn't have this type const rnwStyle = StyleSheet.getSheet() return { ...page, styles: (