Next.jsMedium

What are third-party libraries' compatibility issues with RSC?

Many libraries were written for client-only React. If they use hooks, context, browser APIs or runtime style injection but do not declare "use client", importing them in a Server Component fails. Others pull in Node-only code or are large enough to bloat bundles if imported on the wrong side.

Common problems

  • Missing "use client": UI kits, carousels, animation libraries and form libraries that use hooks. Error: "useState only works in Client Components".
  • Context providers (theme, i18n, state libraries) cannot be rendered by Server Components unless they are client modules.
  • Runtime CSS-in-JS (Emotion, older styled-components setups) needs a style registry with useServerInsertedHTML and works only in Client Components.
  • Browser globals at import time (window access at module scope) crash during server rendering.
  • Node-only or native packages (sharp, bcrypt, Prisma engines) must not be bundled for the client and sometimes not even for the server.
  • Class instances as props (ORM models, Decimal) are not serializable.

Fixes

  1. Wrap in your own client file: "use client"; export { Slider } from "fancy-slider";.
  2. Providers file: create app/providers.tsx with "use client" and render it in the layout.
  3. Browser-only libraries: load with next/dynamic and ssr: false from inside a Client Component.
  4. Native packages: list them in serverExternalPackages so they are loaded from node_modules at runtime. Since 16.1, Turbopack also externalizes their transitive dependencies.
  5. Monorepo or ESM-only packages: use transpilePackages.
  6. Big barrel imports: optimizePackageImports (enabled for many popular libraries automatically).

Checking compatibility

Most popular libraries (MUI, Chakra, Mantine, TanStack, Framer Motion) now ship RSC-compatible entry points. Check their App Router guide.

Code example

tsx
1// app/ui/slider.tsx - wrap a hooks-based library that lacks the directive
2"use client";
3export { Slider } from "fancy-slider";
4
5// app/ui/map.tsx - library touches window at import time
6"use client";
7import dynamic from "next/dynamic";
8export const Map = dynamic(() => import("leaflet-map").then((m) => m.Map), {
9 ssr: false,
10 loading: () => <p>Loading map…</p>,
11});
12
13// next.config.ts
14// const nextConfig = {
15// serverExternalPackages: ["sharp", "@node-rs/argon2"], // native modules
16// transpilePackages: ["@acme/ui"], // monorepo package
17// experimental: { optimizePackageImports: ["my-icon-lib"] },
18// };

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the Next.js set.

Open in study app