storybook: `npx sb init` does not work with NPM v7
Describe the bug
The init script npx sb init does not work with NPM v7.
To Reproduce Steps to reproduce the behavior:
- Use Node v15 with NPM v7
nvm use 15. - Create a new project, e.g.
npx create-react-app my-app. - Go to app directory
cd my-app. - Try to init Storybook
npx sb init. - An error appears:
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: react-storybook@0.1.0
npm ERR! Found: react@17.0.1
npm ERR! node_modules/react
npm ERR! react@"^17.0.1" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer react@"^16.8.0" from @storybook/addon-essentials@6.0.28
npm ERR! node_modules/@storybook/addon-essentials
npm ERR! dev @storybook/addon-essentials@"^6.0.28" from the root project
Expected behavior
I would expect the command npx sb init to add Storybook to the project, same way as when using older versions of NPM.
System
System:
OS: macOS 10.15.7
CPU: (12) x64 Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz
Binaries:
Node: 15.0.1 - ~/.nvm/versions/node/v15.0.1/bin/node
npm: 7.0.3 - ~/.nvm/versions/node/v15.0.1/bin/npm
Browsers:
Chrome: 86.0.4240.111
Firefox: 70.0.1
Safari: 14.0
Additional context I think the problem is caused by a breaking change in NPM v7. It now installs peer dependencies automatically: https://github.com/npm/rfcs/blob/latest/implemented/0025-install-peer-deps.md
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 27
- Comments: 61 (20 by maintainers)
Workaround:
npx --legacy-peer-deps sb initreact 17.0.1 npm 7.5.2 Finally,
npx --legacy-peer-deps sb initworks for me.create-react-context is a polyfill for
React.createContextwhich was introduced in React 16. Storybook (according to package.json) only supports React 16+. So the polyfill shouldn’t be need anyway. BUT you need to look at the whole chain Storybook isn’t requiring create-react-context directly, it’s actually Reach Router that requires it. So first create-react-context needs to be updated (PR), and then Reach Router needs to be updated itself and to use the updated CRC, and then Storybook needs to be updated to use the new Reach Router. 🤝This is annoying but not much can be done other than helping out those projects. 🚀
There’s no finger pointing or blame here, many JS projects are facing the same teething pains with NPM v7 due to package managers previously not bothering to resolve peerDependencies properly. So for years projects have understandably been getting away with broken / outdated / overly strict peer deps and now that suddenly NPM does the right thing it’s highlighting these issues. Hopefully the community will get used to it and we won’t have issues like this again 😄
I can confirm this issue.
I worked with lastest next.js typescript. npm version 7.0.8 node version 15.2.1
I can not install storybook. Neither
npx sb@next initnornpx sb init, error message is the similar like above mentioned.Then I run
npm install -g npm@latest, suddenly my npm would down graded to 6.14.9, then I install the storybook withnpx sb init, then it works.@shilman Still does not work. I have tried this:
It produces this error:
Another workaround (that also works for co-workers that might be unaware of the --legacy-peer-deps flag) is creating a
.npmrcin your root folder with the following line:After that
npm installshould just work (also on CI).If you’re reading this, please go upvote https://github.com/reach/router/pull/436 to let them know it’s important 🙏
I can add my voice to the “workaround doesn’t work” party.
Then after a fresh
npx create-react-app my-app, the suggested workaroundnpx sb@next initfails for me exactly as described by @euan-cowie in the previous comment. (Non-workaround version fails too, of course.)вы так и не решили проблему?
fwiw I’m still experiencing this with react 17 app:
node 15.3.0 npm 7.0.14
resolve error:
Any idea why it’s trying to resolve
react-storybook??Try installing 6.1 instead? We’ve improved the dependency structure for React17 support.
Alright, not sure how it’s solved now, but it is.
for someone has a problem installing storybook on fresh project then try this
npx -p @storybook/cli sb initif no working try this
npm i @storybook/cli npx sb initLast commit in create-react-context is 3 years ago. Is there anyway for us to not depend on it, even indirectly… Storybook is really modern…
Is this a problem with
6.1? You can install that withnpx sb@next init, or upgrade withnpx sb upgrade --prerelease.Same. We cannot integrate storybook into our app even if we do a completely fresh
create-react-appbuild.I have already tried the
--legacy-peer-depsargument but no difference. We have also tried upgrading React but17.0.1and16.14.0produce the same results.npx sb@next initfails with the exact same errors asnpx sb init.works for me too.
@marcoavazquez in npm7
Olé!! I just released https://github.com/storybookjs/storybook/releases/tag/v6.1.21 containing PR #14106 that references this issue. Upgrade today to the
@latestNPM tag to try it out!thanks, it helped, today the only option to make it move 😗
@John118118 Aha thanks for explaining, much appreciated. That makes a lot more sense. I guess we’ll just need to wait for it, or maybe take matters into our own hands if it doesn’t happen in a reasonable amount of time… 🙄
create-react-context is rejecting react 17 as a peer dep in this line in the error stack:
npm ERR! peer react@"^0.14.0 || ^15.0.0 || ^16.0.0" from create-react-context@0.3.0which appears to be the crux of this entire problem. If that PR was to be merged (and released, etc) it would accept it.
I’m assuming NPM v7 has introduced a strict peer dependency check, and the legacy flag uses a/the previous, more lax, checks or something. I can see lots of mentions of strict peer dependency handling in the release commit messages, but I’m still new to the modern JS ecosphere so I can’t be certain without a considerable amount of research 😃
e: more appropriate wording
@John118118 can you elaborate on this? i believe all our peer deps have already been updated to accommodate react 17…
It seems like this might’ve regressed. Can somebody who’s able to reproduce this problem bisect and figure out which
6.1.xversion it started working in, and which version it stopped working in again? Thank you 🙏@John118118 #13280
Fantastic! Thanks @fczbkk for narrowing down the problem and also for your patience as we got the release in order. I’ll close this and reopen if people are still experiencing the issue.
npm@7.0.3 node@v15.0.1 This caused a semi successful installation: 2020-11-05T05_06_17_212Z-debug.log 2020-11-05T05_06_17_269Z-debug.log