storybook: React + Storybook + SCSS modules not working
Describe the bug
Storybook doesn’t build/run with suggested changes for SCSS compatibility with modules.
Edit: Just tested without modules and changing file extension from css to scss also breaks it
To Reproduce
Create a new React app with Typescript (per React)
npx create-react-app my-app --template typescript
Add Storybook (per Storybook)
npx sb init
Add SCSS compatibility (per Storybook) by using SCSS preset
Here’s the repository.
After npm run storybook, this is the error I get:
ModuleBuildError: Module build failed (from ./node_modules/sass-loader/dist/cjs.js):
SassError: expected "{".
╷
2 │ import API from "!../../node_modules/style-loader/dist/runtime/injectStylesIntoStyleTag.js";
│ ^
╵
src\stories\button.module.scss 2:98 root stylesheet
at processResult (C:\workspace\omegafox-components\node_modules\webpack\lib\NormalModule.js:758:19)
at C:\workspace\omegafox-components\node_modules\webpack\lib\NormalModule.js:860:5
at C:\workspace\omegafox-components\node_modules\loader-runner\lib\LoaderRunner.js:399:11
at C:\workspace\omegafox-components\node_modules\loader-runner\lib\LoaderRunner.js:251:18
at context.callback (C:\workspace\omegafox-components\node_modules\loader-runner\lib\LoaderRunner.js:124:13)
at Object.loader (C:\workspace\omegafox-components\node_modules\sass-loader\dist\index.js:69:5)
at runNextTicks (node:internal/process/task_queues:61:5)
at processImmediate (node:internal/timers:437:9)
I’ve tried npx sb@next repro but it didn’t work:

About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 16
- Comments: 37 (3 by maintainers)
Just tried again from scratch, repeating step by step from Storybook docs and it’s still not working. Tried downgrading
sass-loaderto 10.2.1, still not working.It’s a brand new installation of React + Storybook with SASS following the app documentation and it’s broken, it’s kinda nuts that this is still on “needs triage” tag 1 month after the bug was created.
I have a workaround that may help until this is resolved. My project was bootstrapped with create react.
In your package.json add a script for
sassThen add
main.cssto.storybook/preview.jsRun both
npm run sassandnpm run storybookoryarnappropriately in separate terminals.Running Node 16.5.0 + Npm 7.19.1
I followed this step by step: https://github.com/storybookjs/presets/tree/master/packages/preset-scss but it didn’t work. Only after I removed the line
"@storybook/preset-scss", from mymain.jsit worked. Really weird as it’s what it says in the documentation. It must be conflicting with create-react-app or something?My main.js now looks like this:
My file is located in stories and is called
mycomp.scssand is imported inMyComp.jsxasimport "./mycomp.scss";@spaceflora After dicking around with this for what felt like eternity I came across this plugin:
https://github.com/RyanClementsHax/storybook-addon-next
and it solved EVERY problem I was having. It literally works right out the box, with css/scss/global styles AND modules. My main.js file now looks like this:
So if anyone is having trouble getting scss modules and tailwind working with Storybook then give this plugin a go…
After downgrade still same error
this article helped: https://bitcoden.com/answers/using-kebab-case-css-classnames-in-css-or-sass-modules-when-using-next-js
@assentorp’s solution worked for me: I removed the
@storybook/preset-scssaddon frommain.js. I’m using Storybook v7.Can you detail your configuration please ? Do you use any storybook presets / addon ?
This solved my issues after 4 hours of pain. Unbelievable how badly documented Storybook is when it comes to Sass.
I changed the main.js configuration to use the webpackFinal found here. It’s working for global styles, but not for modules. https://storybook.js.org/docs/react/builders/webpack#extending-storybooks-webpack-config I’m sorry for bothering, maybe someone can use it.
@FelipeZNascimento @XavierOlland @xiaozhiwen1998 fixed this way ( .storybook/main.js ). The main point is that you have to setup webpackFinal inside of the core object. I’ve tried with ‘@storybook/preset-scss’ - no luck 😦 Hope that it’ll help you
works for me
It work for me when I removed the configuration of package
storybook-addon-sass-postcssin main.js.in preview.js add: import ‘…/src/styles/index.scss’;
Adding my voice to the discussion to say that I am also having pretty much the same issue with Nextjs/Tailwind. global Scss files are working fine, modules are not…
For anybody coming to this issue on 7.0+, please take a look at https://storybook.js.org/addons/@storybook/addon-styling which is our recommended way to deal with styling integrations.