Skip to content

React: Fix runtime TypeScript import crash in non-TypeScript projects#34341

Draft
Copilot wants to merge 2 commits intonextfrom
copilot/fix-runtime-import-issue
Draft

React: Fix runtime TypeScript import crash in non-TypeScript projects#34341
Copilot wants to merge 2 commits intonextfrom
copilot/fix-runtime-import-issue

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 26, 2026

@storybook/react was crashing with ERR_MODULE_NOT_FOUND: Cannot find package 'typescript' in projects that don't have TypeScript installed, because getTsConfig in reactDocgen/utils.ts unconditionally called await import('typescript') before checking whether a tsconfig.json even existed. An accidental console.log({ tsconfigPath }) was also left in.

Changes

  • reactDocgen/utils.ts: Refactored getTsConfig to use findTsconfigPath (backed by find-up, no TypeScript required) as a pre-check before dynamically importing TypeScript:
    • No tsconfig.json → returns {} immediately, TypeScript is never imported
    • tsconfig.json found → imports TypeScript and parses compiler options
    • TypeScript not installed but tsconfig.json exists → caught by the existing try/catch, returns {}
  • Removed the stray console.log({ tsconfigPath }) debug artifact
// Before: always imports typescript first
const ts = await import('typescript');
const tsconfigPath = ts.findConfigFile(process.cwd(), ts.sys.fileExists);
console.log({ tsconfigPath }); // leftover debug

// After: check for tsconfig without touching TypeScript
const tsconfigPath = findTsconfigPath(process.cwd());
if (!tsconfigPath) return {};
const ts = await import('typescript'); // only reached if tsconfig.json exists
Original prompt

This section details on the original issue you should resolve

<issue_title>[Bug]: @storybook/react runtime import of typescript breaks when TS is not installed</issue_title>
<issue_description>### Describe the bug

While running Storybook in a project that does not use TypeScript, we’re hitting a runtime error caused by a missing typescript dependency.

Error

Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'typescript'

Either:

  • typescript should be declared as a dependency (or peerDependency) if it's required at runtime
  • The import should be optional / lazy / guarded so that non-TypeScript projects can run without it

Reproduction link

N/A

Reproduction steps

Package: @storybook/nextjs
Version: 10.3.3
Transitive dependency: @storybook/react
Environment: non-TypeScript project (no typescript installed)

System

│  Storybook Environment Info:
│

│  System:
│  OS: macOS 26.3.1
│  CPU: (10) arm64 Apple M1 Pro
│  Shell: 5.9 - /bin/zsh
│  Binaries:
│  Node: 24.8.0 - /opt/homebrew/bin/node
│  Yarn: 1.22.22 - /opt/homebrew/bin/yarn <----- active
│  npm: 11.6.0 - /opt/homebrew/bin/npm
│  pnpm: 10.28.2 - /opt/homebrew/bin/pnpm
│  Browsers:
│  Safari: 26.3.1

Additional context

  • This seems to come from the build output rather than explicit source usage
  • Likely related to docgen / TypeScript tooling being bundled
  • My guess is this goes unnoticed because most projects already have typescript installed

Workaround

Installing typescript fixes the issue for the version 10.3.3:

npm install -D typescript

Suggestion

It might be worth reviewing how typescript is bundled / referenced in @storybook/react to avoid requiring it implicitly at runtime.</issue_description>

Comments on the Issue (you are @copilot in this section)


📍 Connect Copilot coding agent with Jira, Azure Boards or Linear to delegate work to Copilot in one click without leaving your project management tool.

…resent

Co-authored-by: valentinpalkovic <5889929+valentinpalkovic@users.noreply.github.com>
Agent-Logs-Url: https://github.com/storybookjs/storybook/sessions/742b42b0-e301-4681-a8b2-30996b1ddd76
Copilot AI changed the title [WIP] Fix runtime import issue in Storybook for non-TypeScript projects React: Fix runtime TypeScript import crash in non-TypeScript projects Mar 26, 2026
Copilot AI requested a review from valentinpalkovic March 26, 2026 12:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: @storybook/react runtime import of typescript breaks when TS is not installed

2 participants