Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use scss without css modules in nextjs

I can configure webpack to allow includes of scss.

Note: Working with classNames on the original html is faster when copying code between static html to React components, which is why I want to do it this way.

How to make this work in nextjs without css modules and styled components and just using basic scss and classnames?


import 'styles/MyComponent.scss';

const MyComponent = () => {
  return (
    <div className="someClass">
      stuff..
    </div>
  );
};

export default MyComponent;
like image 836
Steve Tomlin Avatar asked Jan 30 '26 02:01

Steve Tomlin


1 Answers

  1. install sass
npm install --save-dev sass
  1. Disable css-modules component styling in Next.js

In the next.config.js, add the following config:

/** @type {import('next').NextConfig} */
const path = require('path');
const nextConfig = {
  // disable css-modules component styling
  webpack(config) {
    config.module.rules.forEach((rule) => {
      const { oneOf } = rule;
      if (oneOf) {
        oneOf.forEach((one) => {
          if (!`${one.issuer?.and}`.includes('_app')) return;
          one.issuer.and = [path.resolve(__dirname)];
        });
      }
    })
    return config;
  },
}

module.exports = nextConfig

Verified in next.js version: v12.3~v13.x

See example: No CSS modules in next.js app: (json5 app) No CSS modules in next.js

like image 125
cwtuan Avatar answered Jan 31 '26 20:01

cwtuan