storybook: Cannot find module 'babel-loader/package.json'
Describe the bug
When running storybook yarn storybook after upgrading to 4 I’m getting these errors:
Error: Cannot find module 'babel-loader/package.json'
at module.exports
at isBabelLoader8
at _default
at Object.babel
at accumulationPromise.then.newConfig
at process.internalTickCallback
ERROR in ./.storybook/addons.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
Error: Cannot find module 'babel-preset-react' from '/Users/path'
- If you want to resolve "react", use "module:react"
- Did you mean "@babel/react"?
System:
- OS: MacOS
- Device: Macbook Pro 2016
- Framework: react
- Version: 4.0.2
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 22 (5 by maintainers)
Commits related to this issue
- Hack to get Storybook to work with CRA https://github.com/storybookjs/storybook/issues/4672 — committed to InseeFrLab/onyxia by garronej 4 years ago
- Hack to get Storybook to work with CRA https://github.com/storybookjs/storybook/issues/4672 — committed to InseeFrLab/onyxia by garronej 4 years ago
babel-loaderis now a peer dep. Take a look at https://github.com/storybooks/storybook/blob/master/MIGRATION.mdThis is still an issue, if you add
babel-loaderwhen using create-react-app, and it’s not the expected version, then you’ll receive the following:I solved it by adding
@babel/coreandbabel-loaderto mydevDependencies. You can also achieve this by runningnpx -p @storybook/cli sb init -fnotice the -f option, this will overwrite your Storybook config, but you can easily change it back by reverting the change to the config.I also got “Error: Cannot find module ‘babel-loader/package.json’” when running
yarn run storybook.Adding babel-loader seems to fix it
yarn add -D babel-loaderBut I am a bit surprised that I needed to do this step after running
npx storybookIf you change the your custom Storybook
webpack.config.jsto resolve babel-loader via:loader: require.resolve('babel-loader', { paths: [ 'node_modules/react-scripts' ]}),it starts working for a longer term solution until it’s actually fixed.
@mrmckeb I would not mind if it was included as its own dependency, but forcing it be the used at the top level is not desirable. One of the main reasons I use Typescript is to avoid having to deal with Babel at all, so I would prefer if this was a dependency of Storybook and not a peer dependency. Other popular libraries (Jest, etc) include these internal tools as deps, not peerDeps (I don’t have to install Mocha alongside Jest, etc). Peer dependencies add an extra layer of confusion and frustration that is not needed IMHO.
Pls. reopen as it will break after each
react-scriptsandstorybookdep change.Hi,
having the same issue. The project uses Babel 7.
See https://github.com/pascalduez/react-module-boilerplate
So the migration guide is actually misleading:
Actually the loader must be installed in any case, whether using Babel or not.
@mrmckeb Is what @ivan-kleshnin said true, or are we able to use the one out of
react-scriptsnow?@falnos24865 Unfortunately it’s needed by Storybook itself. I personally wouldn’t consider it a blocker - if it was included by Storybook as a dependency, it would still be there - the way it is now, users have control over the exact version of
babel-loaderused.@TimonVS this breaks
yarn testdue to CRA recognizing a self-declared dependency the project has already installed for those who are using testing in their app.@falnos24865, if it is not installed at the top level, it can conflict with other installed versions of the loader. We’ve seen this with Creact React App as an example.
You’ll note that most dependencies are handled as you suggest. This is about managing versions effectively.
It works if you install the exact expected version from both react scripts and storybook:
Hi, I followed @jasperdunn and @TimonVS ’ comments and I was able to get storybook up running and the react app up and running using
npx create-react-app taskboxandnpx -p @storybook/cli sb init. Just like @caseybaggz said, this breaksyarn test.How should I go about resolving this? If I follow the instructions that come up from the error messages, i.e., removing babel-loader from package.json, my storybook breaks.
EDIT: this is also a known issue in #5183 and the temp solution to my problem was https://github.com/storybooks/storybook/issues/5183#issuecomment-454333915
My team was using .babelrc Doing some research and poking around, I changed the file from
to
this fixed the issue for me 😃