diff --git a/code/core/src/core-server/load.ts b/code/core/src/core-server/load.ts index 0c0d716dce37..28b198bc04c4 100644 --- a/code/core/src/core-server/load.ts +++ b/code/core/src/core-server/load.ts @@ -11,7 +11,7 @@ import type { BuilderOptions, CLIOptions, LoadOptions, Options } from 'storybook import { global } from '@storybook/global'; -import { dirname, join, relative, resolve } from 'pathe'; +import { dirname, isAbsolute, join, relative, resolve } from 'pathe'; import { resolvePackageDir } from '../shared/utils/module'; @@ -68,7 +68,13 @@ export async function loadStorybook( const builderName = typeof builder === 'string' ? builder : builder?.name; if (builderName) { - corePresets.push(join(dirname(builderName), 'preset.js')); + /* builderName can be a bare package name (e.g. '@storybook/builder-vite') or an already-resolved + file URL / absolute path (e.g. 'file:///.../.../dist/index.js'). For bare package names, we + need to resolve the package directory first; for already-resolved paths, dirname works directly. + */ + const isResolved = builderName.startsWith('file:') || isAbsolute(builderName); + const builderPresetDir = isResolved ? dirname(builderName) : resolvePackageDir(builderName); + corePresets.push(join(builderPresetDir, 'preset.js')); } // Load second pass: all presets are applied in order diff --git a/code/core/src/shared/utils/module.ts b/code/core/src/shared/utils/module.ts index 7183241abdf0..960e3fcef645 100644 --- a/code/core/src/shared/utils/module.ts +++ b/code/core/src/shared/utils/module.ts @@ -32,8 +32,14 @@ export const resolvePackageDir = ( try { return dirname(fileURLToPath(importMetaResolve(join(pkg, 'package.json'), parent))); } catch { - // Necessary fallback for Bun runtime - return dirname(fileURLToPath(importMetaResolve(join(pkg, 'package.json')))); + try { + // Necessary fallback for Bun runtime + return dirname(fileURLToPath(importMetaResolve(join(pkg, 'package.json')))); + } catch { + // Fallback using require.resolve for strict pnpm environments where import.meta.resolve may fail + const req = createRequire(parent ?? import.meta.url); + return dirname(req.resolve(join(pkg, 'package.json'))); + } } };