storybook: Cannot init in a new project
Describe the bug
I am trying to install Storybook in a new project. But when I run npx storybook@latest init, it says “Error: Unable to find versions of @storybook/cli using npm”
To Reproduce
Run npx storybook@latest init
System
Storybook Environment Info:
System:
OS: Linux 6.5 Ubuntu 22.04.4 LTS 22.04.4 LTS (Jammy Jellyfish)
CPU: (8) x64 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
Shell: 5.1.16 - /bin/bash
Browsers:
Chrome: 122.0.6261.128
Additional context
Error: Unable to find versions of @storybook/cli using npm
at NPMProxy.runGetVersions (/home/pavitra/.npm/_npx/6fe9a9991b157df1/node_modules/@storybook/core-common/dist/index.js:37:639)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async doInitiate (/home/pavitra/.npm/_npx/6fe9a9991b157df1/node_modules/@storybook/cli/dist/generate.js:146:997)
at async withTelemetry (/home/pavitra/.npm/_npx/6fe9a9991b157df1/node_modules/@storybook/core-server/dist/index.js:28:3579)
at async initiate (/home/pavitra/.npm/_npx/6fe9a9991b157df1/node_modules/@storybook/cli/dist/generate.js:180:250)
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 9
- Comments: 60 (17 by maintainers)
I think you should
npm install @storybook/clifirst.Hey everyone, this error seems to be related to a bug in a specific NPM versions (see this confirmation), plus our error handling that didn’t really provide much information.
In Storybook 8.2 the error message will be updated to contain the full error to assist us in fixing this. In case there is something related other than the NPM bug, we will take a look and fix.
If you are still experiencing this issue, please:
node --version+npm --version(write down to share here).I’ve been having the same issue and came here to find a solution, but since none have been posted, I just decided to look into it myself.
The problem seems to stem from a command executed that runs
npm info storybook version --jsonand parses it as JSON. This command currently returns8.4.2(which is not a valid JSON and throws an error). Note that if the latest version was didn’t have a build version such as8.4it would have passed since this is valid JSON (number).Here’s the specific area that fails in /core/src/common/js-package-manager/NPMProxy.ts#L238-L264 along with the suggested fix
The same command in pnpm also returns the same semantic version which when parsed as JSON will also fail. I’ll submit a PR for this unless someone else get to it before I do.
EDIT: Looks like the root cause is related to npm. Older versions would output
npm info storybook version --jsonas"8.4.2"(quoted string)Still getting the error on
Node: 22.0.9 npm: 10.8.3 Storybook: 8.3.3
Confirming, upgrading npm to 10.8.1 resolved the issue when using
pnpm.👋 I am Execa’s co-maintainer.
From the look of it, I do not think there is a bug with Execa there, but the issue at https://github.com/sindresorhus/execa/issues/995 can track this.
In terms of calling Execa in Storybook, I would recommend replacing
execaCommand()withexeca()instead.execaCommand()is intended for REPL, not programmatic usage. This documentation explains it. The current usage in Storybook might not be escaping arguments properly due to this.More details: the
shell: trueoption executes the command and its argument as a single string, with the shell interpreting it. TheexecaCommand()splits the input string into a command and an arguments array using spaces as delimiters. And Storybook joins the command and the arguments array into a string. So right now, this looks this:As you can see, the
execaCommand()'s parsing is not really needed there, andexeca()could be called directly instead.The
shell: trueoption is usually problematic, as it prevents proper escaping (as you see from the above example with spaces, which end up not being quoted), which could look to command injection. However,npmandyarnare weird, and might requires being running in a shell, depending on your case.https://github.com/storybookjs/storybook/blob/7e1ee32d46075bed700b4ae193e54546b5a9b0e2/code/lib/core-common/src/js-package-manager/JsPackageManager.ts#L566-L574
Has anyone found a solution?
Confirming upgrade to npm 10.8.1 resolved the issue for me, as well. Thank you @yannbf!
I found that a lot of the
npxcommands, includinginit, does not work with Node 22. It worked after I downgraded Node to version 20. I made an issue here (now closed): https://github.com/storybookjs/storybook/issues/29673 (reproducible in Docker).I noticed that most people here are using Node 22. The error I had is slightly different though (no error, just hangs indefinitely).
Facing this issue as well, on an Ubuntu 24.04, I had my node installation managed by snap. I removed the node installation from snap, and installed node through nvm - once I did that everything worked as expected
@vanessayuenn appears that this is still an issue for some users, even on newer versions.
I’ll try to fix and send a PR.
Looks like this has been resolved then! Thanks everyone for confirming the fix.
@lfrancke both are installed on my system.
@lfrancke Until this is fixed, you have to patch the Storybook CLI files as mentioned in one of my comment above… I remember that I had to return a hardcoded version string so that the
JSON.parsedoesn’t fail.https://github.com/storybookjs/storybook/issues/26553#issuecomment-2082636672
@pavitra-infocusp re-opened. so i guess we wait for the resolution in execa? thanks so much for digging in on this. 🙏
npx installs the CLI if it isn’t already available. Also, nowhere in the getting started guide it is mentioned that the CLI needs to be installed separately before.