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)

Most upvoted comments

I think you should npm install @storybook/cli first.

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:

  • Check which version of Node and NPM you are using: node --version + npm --version (write down to share here).
  • If you are using npm 10.8.0, upgrade to another version if you can
  • If your issue still happens after upgrading, share as much information as you can 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 --json and parses it as JSON. This command currently returns 8.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 as 8.4 it 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

  protected async runGetVersions<T extends boolean>(
    packageName: string,
    fetchAllVersions: T
  ): Promise<T extends true ? string[] : string> {
-    const args = [fetchAllVersions ? 'versions' : 'version', '--json'];
+    const args = [fetchAllVersions ? 'versions --json' : 'version'];
    try {
      const commandResult = await this.executeCommand({
        command: 'npm',
        args: ['info', packageName, ...args],
      });

-      const parsedOutput = JSON.parse(commandResult);
+      const parsedOutput = fetchAllVersions ? JSON.parse(commandResult) : commandResult;

      if (parsedOutput.error?.summary) {
        // this will be handled in the catch block below
        throw parsedOutput.error.summary;
      }

      return parsedOutput;
    } catch (error) {
      throw new FindPackageVersionsError({
        error,
        packageManager: 'NPM',
        packageName,
      });
    }
  }

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 --json as "8.4.2" (quoted string)

Still getting the error on

Node: 22.0.9 npm: 10.8.3 Storybook: 8.3.3

SB_CLI_0001 (FindPackageVersionsError): Unable to find versions of "storybook" using NPM
Reason: SyntaxError: Unexpected non-whitespace character after JSON at position 3
    at _F.runGetVersions (/Users/edmacovaz/.npm/_npx/3158839f1cfed546/node_modules/@storybook/core/dist/common/index.cjs:142627:13)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async doInitiate (/Users/edmacovaz/.npm/_npx/3158839f1cfed546/node_modules/create-storybook/dist/bin/index.cjs:85:995)
    at async withTelemetry (/Users/edmacovaz/.npm/_npx/3158839f1cfed546/node_modules/@storybook/core/dist/core-server/index.cjs:47080:12)
    at async initiate (/Users/edmacovaz/.npm/_npx/3158839f1cfed546/node_modules/create-storybook/dist/bin/index.cjs:119:214) {
  data: {
    error: SyntaxError: Unexpected non-whitespace character after JSON at position 3
        at JSON.parse (<anonymous>)
        at _F.runGetVersions (/Users/edmacovaz/.npm/_npx/3158839f1cfed546/node_modules/@storybook/core/dist/common/index.cjs:142622:20)
        at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
        at async doInitiate (/Users/edmacovaz/.npm/_npx/3158839f1cfed546/node_modules/create-storybook/dist/bin/index.cjs:85:995)
        at async withTelemetry (/Users/edmacovaz/.npm/_npx/3158839f1cfed546/node_modules/@storybook/core/dist/core-server/index.cjs:47080:12)
        at async initiate (/Users/edmacovaz/.npm/_npx/3158839f1cfed546/node_modules/create-storybook/dist/bin/index.cjs:119:214),
    packageManager: 'NPM',
    packageName: 'storybook'
  },
  fromStorybook: true,
  category: 'CLI',
  documentation: false,
  code: 1
}

Confirming, upgrading npm to 10.8.1 resolved the issue when using pnpm.

> nvm install 20
> node --version
v20.15.0
>  npm --version
10.8.1
> pnpm dlx storybook@latest init

👋 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() with execa() 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: true option executes the command and its argument as a single string, with the shell interpreting it. The execaCommand() 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:

runCommand({command: 'npm', arguments: ['install', 'example with space']});
-> execaCommand('npm install example with space', {shell: true});
-> execa('npm', ['install', 'example with space'], {shell: true});
-> child_process.spawn('npm', ['install', 'example with space'], {shell: true});
-> (on Unix) bash -c "npm install example with space"
   (on Windows) cmd /d /s /c "npm install example with space"

As you can see, the execaCommand()'s parsing is not really needed there, and execa() could be called directly instead.

The shell: true option 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, npm and yarn are 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!

$ npm i -g npm@10.8.1
$ npx storybook@latest init

I found that a lot of the npx commands, including init, 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.parse doesn’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. 🙏

I think you should npm install @storybook/cli first.

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.