Next.jsEasy
How do you use Sass in Next.js?
Install the sass package and import .scss or .sass files directly: global stylesheets in the root layout, and .module.scss files in components for scoped styles. Configure shared variables or options with sassOptions in next.config.ts.
Setup
npm install --save-dev sass- Global:
import "./globals.scss"inapp/layout.tsx. - Scoped:
import styles from "./button.module.scss".
sassOptions
additionalData: '@use "@/styles/tokens" as *;'to inject variables or mixins into every file.includePathsorloadPathsfor import resolution.implementation: "sass-embedded"to use the faster embedded compiler.silenceDeprecationsto quiet known warnings during migration.
Modern Sass
- Use
@useand@forwardinstead of the deprecated@import. - Next.js 16 upgraded to
sass-loaderv16 with the modern Sass API. - Turbopack does not support the
~prefix fornode_modulesimports. Write@use "bootstrap/scss/bootstrap"instead of~bootstrap/..., or add aturbopack.resolveAlias.
Exporting variables to JavaScript
CSS Modules support :export { primary: $primary; }, so you can import Sass values in TypeScript.
Sass vs native CSS
Native CSS now has nesting, custom properties, color-mix() and container queries. Many teams use plain CSS (or Tailwind) for new projects, and keep Sass for existing design systems.
Code example
tsx
1// next.config.ts2import type { NextConfig } from "next";34const nextConfig: NextConfig = {5 sassOptions: {6 additionalData: `@use "@/styles/tokens" as *;`, // tokens available everywhere7 implementation: "sass-embedded",8 },9};10export default nextConfig;1112/* styles/_tokens.scss */13// $brand: #4f46e5;14// @mixin card { border-radius: 12px; padding: 1rem; }1516/* app/components/card.module.scss */17// .card {18// @include card;19// border: 1px solid rgba($brand, 0.2);20// &:hover { border-color: $brand; }21// }22// :export { brand: $brand; }2324// app/components/card.tsx25import styles from "./card.module.scss";26export function Card({ children }: { children: React.ReactNode }) {27 return <div className={styles.card} data-brand={styles.brand}>{children}</div>;28}
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.