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.
The CLI can wrap your build command, optimizing files beforehand and restoring
them after:
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:
Terminal
# 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
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.
Use --expect-optimizations to fail builds if the compiler optimizes fewer than
the expected minimum number of components:
{
"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.
yarn
npm
bun
pnpm
yarn add @tamagui/next-theme
Here’s how to set up your NextTamaguiProvider.tsx:
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.
SwitchThemeButton.tsx
// copy-paste: these skins import from files you own. copy each registry item
// below into your app, then adjust the relative import paths to fit.
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 for more options.
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.
With outputCSS, you don’t need getCSS() in your _document.tsx; all styles are
handled by the static CSS file and runtime style injection.
App Router (Webpack)
Tamagui includes Server Components support for the Next.js app directory with
use client
support.
Note that "use client" components do render on the server, and since Tamagui
extracts to CSS statically and uses inline <style /> tags for non-static
styling, you get excellent performance out of the box.
The Tamagui plugin is optional but helps with compatibility with the rest of the
React Native ecosystem. It requires CommonJS for now because the optimizing
compiler uses various resolving features that haven’t been ported to ESM yet.
Rename your next.config.mjs to next.config.js before adding it:
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.
yarn
npm
bun
pnpm
yarn add @tamagui/next-theme
Here’s how to set up your NextTamaguiProvider.tsx:
Then link the generated CSS file in your app/layout.tsx:
app/layout.tsx
import'../public/tamagui.generated.css'
With React 19, Tamagui automatically injects runtime styles via style tags on the
server. The outputCSS file handles themes and tokens generated at build time, so you
don’t need any getCSS() calls in your provider.