Integrating Sass/SCSS in Next.js
Next.js has built-in Sass support, letting you use variables, mixins, and nesting. Import .scss files for component-scoped styles or global stylesheets. The main footgun: you must install the `sass` package yourself; it's not included by default.
WHY IT EXISTS Plain CSS becomes difficult to maintain in large applications. It lacks features like variables for theme colors, nesting for clear structure, and mixins for reusable code blocks. This leads to repetitive, disorganized, and hard-to-debug stylesheets. Sass was created to add these programmatic capabilities to CSS.
THE MENTAL MODEL Think of Sass in Next.js as CSS with superpowers, enabled by a single command. Once you install the sass package, Next.js automatically detects and compiles your .scss or .sass files into browser-readable CSS during the build process. There's no need to manually configure webpack or other build tools; it just works.
HOW IT WORKS First, you must install the necessary compiler by running npm install -D sass in your project. After installation, you can create files with .scss or .sass extensions and import them directly into your JavaScript or TypeScript files. For component-scoped styles, use the [name].module.scss naming convention. For global styles that apply to your entire application, create a file like globals.scss and import it only once in your root layout file (app/layout.js or pages/_app.js).
WHEN TO USE IT Use Sass when your project's styling needs organization and scalability. It is ideal for managing a design system with variables, creating reusable UI patterns with mixins (e.g., for buttons or cards), and keeping your styles logically nested to match your component hierarchy. It's a strong choice for any project expected to grow in complexity.
WHEN NOT TO USE IT For small prototypes or single-page sites with minimal styling, plain CSS or CSS Modules may be sufficient and avoids an extra dependency. If your team is already standardized on a different styling solution like Tailwind CSS or a CSS-in-JS library, introducing Sass can add unnecessary complexity to the tech stack.
ONE CANONICAL EXAMPLE To configure Sass options, you can use the sassOptions key in your next.config.js file. For instance, to make your Sass variables available in every component module without manual imports, you can add a path to your variables file. In next.config.js, you might set sassOptions to include additionalData: "@import 'styles/variables.scss';". This automatically prepends the import to every processed SCSS file.
Read the original → nextjs.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.