From 1f6a898e1fb482c215a1bf5abf3a0d1ff9f30716 Mon Sep 17 00:00:00 2001 From: Norbert de Langen Date: Wed, 14 Jan 2026 11:42:29 +0100 Subject: [PATCH 1/3] detect free port when running dev during initiate, to ensure the server starts without asking --- code/lib/create-storybook/src/initiate.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/code/lib/create-storybook/src/initiate.ts b/code/lib/create-storybook/src/initiate.ts index 25780e25a4f5..253643b12225 100644 --- a/code/lib/create-storybook/src/initiate.ts +++ b/code/lib/create-storybook/src/initiate.ts @@ -8,6 +8,8 @@ import { withTelemetry } from 'storybook/internal/core-server'; import { logTracker, logger } from 'storybook/internal/node-logger'; import { ErrorCollector } from 'storybook/internal/telemetry'; +import detectFreePort from 'detect-port'; + import { executeAddonConfiguration, executeDependencyInstallation, @@ -183,6 +185,15 @@ async function runStorybookDev(result: { flags.push('--silent'); } + // Check if default port 6006 is available + const defaultPort = 6006; + const availablePort = await detectFreePort(defaultPort); + const useAlternativePort = availablePort !== defaultPort; + + if (useAlternativePort) { + flags.push(`--port=${availablePort}`); + } + // npm needs extra -- to pass flags to the command // in the case of Angular, we are calling `ng run` which doesn't need the extra `--` const doesNeedExtraDash = @@ -203,7 +214,7 @@ async function runStorybookDev(result: { // executed directly in the user's project directory. This avoid potential issues // with packages running in npxs' node_modules const [command, ...args] = [...storybookCommand.split(' '), ...flags]; - executeCommand({ + await executeCommand({ command: command, args, stdio: 'inherit', From f91b2e0cd0585bc021563f7188a698639656c5d3 Mon Sep 17 00:00:00 2001 From: Norbert de Langen Date: Wed, 14 Jan 2026 11:58:53 +0100 Subject: [PATCH 2/3] re-use logic from core --- code/core/src/core-server/index.ts | 2 ++ code/lib/create-storybook/src/initiate.ts | 7 ++----- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/code/core/src/core-server/index.ts b/code/core/src/core-server/index.ts index 5877ab6dab9f..f475fa6166ca 100644 --- a/code/core/src/core-server/index.ts +++ b/code/core/src/core-server/index.ts @@ -30,3 +30,5 @@ export { fullTestProviderStore as internal_fullTestProviderStore, universalTestProviderStore as internal_universalTestProviderStore, } from './stores/test-provider'; + +export { getServerPort } from './utils/server-address'; diff --git a/code/lib/create-storybook/src/initiate.ts b/code/lib/create-storybook/src/initiate.ts index 253643b12225..03db782644c3 100644 --- a/code/lib/create-storybook/src/initiate.ts +++ b/code/lib/create-storybook/src/initiate.ts @@ -4,11 +4,8 @@ import { PackageManagerName, executeCommand, } from 'storybook/internal/common'; -import { withTelemetry } from 'storybook/internal/core-server'; +import { getServerPort, withTelemetry } from 'storybook/internal/core-server'; import { logTracker, logger } from 'storybook/internal/node-logger'; -import { ErrorCollector } from 'storybook/internal/telemetry'; - -import detectFreePort from 'detect-port'; import { executeAddonConfiguration, @@ -187,7 +184,7 @@ async function runStorybookDev(result: { // Check if default port 6006 is available const defaultPort = 6006; - const availablePort = await detectFreePort(defaultPort); + const availablePort = await getServerPort(defaultPort); const useAlternativePort = availablePort !== defaultPort; if (useAlternativePort) { From 7781dea1d3bb6a2b2987c8b2ccbed8313afdf2cb Mon Sep 17 00:00:00 2001 From: Norbert de Langen Date: Wed, 14 Jan 2026 12:10:25 +0100 Subject: [PATCH 3/3] append after possible double dash --- code/lib/create-storybook/src/initiate.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/code/lib/create-storybook/src/initiate.ts b/code/lib/create-storybook/src/initiate.ts index 03db782644c3..3377dc488495 100644 --- a/code/lib/create-storybook/src/initiate.ts +++ b/code/lib/create-storybook/src/initiate.ts @@ -182,15 +182,6 @@ async function runStorybookDev(result: { flags.push('--silent'); } - // Check if default port 6006 is available - const defaultPort = 6006; - const availablePort = await getServerPort(defaultPort); - const useAlternativePort = availablePort !== defaultPort; - - if (useAlternativePort) { - flags.push(`--port=${availablePort}`); - } - // npm needs extra -- to pass flags to the command // in the case of Angular, we are calling `ng run` which doesn't need the extra `--` const doesNeedExtraDash = @@ -205,6 +196,15 @@ async function runStorybookDev(result: { flags.push('--initial-path=/onboarding'); } + // Check if default port 6006 is available + const defaultPort = 6006; + const availablePort = await getServerPort(defaultPort); + const useAlternativePort = availablePort !== defaultPort; + + if (useAlternativePort) { + flags.push(`--port=${availablePort}`); + } + flags.push('--quiet'); // instead of calling 'dev' automatically, we spawn a subprocess so that it gets