Repository: storybook
Issue: [Bug]: @storybook/nextjs resolves wrong version of Next.js in monorepo
Labels: bug,nextjs,monorepos,sev:S3

### Describe the bug

I'm encountering an issue in a monorepo containing multiple versions of Next.js where `@storybook/nextjs` references the wrong version of Next.js. 
The setup is as follows:

- Application A: Next.js v12.3.4
- Application B: Next.js v13.5.6
- components: Next.js v13.5.6

File tree:
```
.
├── apps
│  ├── a
│  │  ├── base.json
│  │  ├── package.json  // Next.js v12.3.4
│  │  ├── src
│  │  │  └── index.ts
│  │  └── tsconfig.json
│  └── b
│      ├── base.json
│      ├── package.json  // Next.js v13.5.6
│      ├── src
│      │  └── Sample
│      │      ├── Sample.stories.tsx
│      │      └── Sample.tsx
│      └── tsconfig.json
├── package.json
├── packages
│  └── components // <--- Launch Storybook here
│      ├── base.json
│      ├── package.json  // Next.js v13.5.6
│      ├── src
│      │  ├── LegacyImage
│      │  │  ├── LegacyImage.tsx
│      │  │  └── index.ts
│      │  ├── Sample
│      │  │  ├── Sample.stories.tsx
│      │  │  └── Sample.tsx
│      │  └── index.ts
│      └── tsconfig.json
├── pnpm-lock.yaml
└── pnpm-workspace.yaml
```

When I attempt to launch Storybook for the `components`, I receive the following message:

```
Error: Cannot find module 'next/legacy/image'
```

`next/legacy/image` does not exist in v12, this error is expected. However, it's weird.
Because version 13 is installed in the `components` package.
Why does `@storybook/nextjs` refer to version 12?

The situation improves when the A and B versions are swapped:

- Application A: v13.5.6
- Application B: v12.3.4
- Components: v13.5.6
(All `node_modules` have been deleted and reinstalled.)

This suggests the hypothesis that `@storybook/nextjs` might be referencing the Next.js version from the project that appears first in sort order. 
However, I'm uncertain whether this issue originates from Storybook, pnpm, or another source.

### To Reproduce

Here is reproduction repository.

https://github.com/yuike/repro-cannot-find-module-next-legacy-image

1. Install dependencies.
```
pnpm i
```

2. Launch `components`'s storybook.
```
pnpm -F components storybook:dev
```

3. Error occured.

```
info => Using implicit CSS loaders
=> Failed to build the preview
Error: Cannot find module 'next/legacy/image'
Require stack:
- /Users/${username}/monorepo-sample/node_modules/.pnpm/@storybook+nextjs@7.6.12_@swc+core@1.3.107_esbuild@0.18.20_next@12.3.4_react-dom@18.2.0_react_pvihwtoigpmcj4il242wepk5qe/node_modules/@storybook/nextjs/dist/preset.js
- /Users/${username}/monorepo-sample/node_modules/.pnpm/@storybook+core-common@7.6.12/node_modules/@storybook/core-common/dist/index.js
- /Users/${username}/monorepo-sample/node_modules/.pnpm/@storybook+telemetry@7.6.12/node_modules/@storybook/telemetry/dist/index.js
- /Users/${username}/monorepo-sample/node_modules/.pnpm/@storybook+cli@7.6.12/node_modules/@storybook/cli/dist/generate.js
- /Users/${username}/monorepo-sample/node_modules/.pnpm/@storybook+cli@7.6.12/node_modules/@storybook/cli/bin/index.js
- /Users/${username}/monorepo-sample/node_modules/.pnpm/storybook@7.6.12/node_modules/storybook/index.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1134:15)
    at Module._resolveFilename (/Users/${username}/monorepo-sample/node_modules/.pnpm/esbuild-register@3.5.0_esbuild@0.18.20/node_modules/esbuild-register/dist/node.js:4799:36)
    at Function.resolve (node:internal/modules/helpers:188:19)
    at configureImageDefaults (/Users/${username}/monorepo-sample/node_modules/.pnpm/@storybook+nextjs@7.6.12_@swc+core@1.3.107_esbuild@0.18.20_next@12.3.4_react-dom@18.2.0_react_pvihwtoigpmcj4il242wepk5qe/node_modules/@storybook/nextjs/dist/preset.js:1:15261)
    at configureImages (/Users/${username}/monorepo-sample/node_modules/.pnpm/@storybook+nextjs@7.6.12_@swc+core@1.3.107_esbuild@0.18.20_next@12.3.4_react-dom@18.2.0_react_pvihwtoigpmcj4il242wepk5qe/node_modules/@storybook/nextjs/dist/preset.js:1:14785)
    at Object.webpackFinal (/Users/${username}/monorepo-sample/node_modules/.pnpm/@storybook+nextjs@7.6.12_@swc+core@1.3.107_esbuild@0.18.20_next@12.3.4_react-dom@18.2.0_react_pvihwtoigpmcj4il242wepk5qe/node_modules/@storybook/nextjs/dist/preset.js:1:32047)
    at async Object.webpack (/Users/${username}/monorepo-sample/node_modules/.pnpm/@storybook+builder-webpack5@7.6.12_esbuild@0.18.20_typescript@4.9.5/node_modules/@storybook/builder-webpack5/dist/presets/custom-webpack-preset.js:1:3214)
    at async starter (/Users/${username}/monorepo-sample/node_modules/.pnpm/@storybook+builder-webpack5@7.6.12_esbuild@0.18.20_typescript@4.9.5/node_modules/@storybook/builder-webpack5/dist/index.js:1:7688)
    at async Module.start (/Users/${username}/monorepo-sample/node_modules/.pnpm/@storybook+builder-webpack5@7.6.12_esbuild@0.18.20_typescript@4.9.5/node_modules/@storybook/builder-webpack5/dist/index.js:1:11672)
    at async storybookDevServer (/Users/${username}/monorepo-sample/node_modules/.pnpm/@storybook+core-server@7.6.12/node_modules/@storybook/core-server/dist/index.js:66:2459)
    at async buildOrThrow (/Users/${username}/monorepo-sample/node_modules/.pnpm/@storybook+core-server@7.6.12/node_modules/@storybook/core-server/dist/index.js:60:8290)
    at async buildDevStandalone (/Users/${username}/monorepo-sample/node_modules/.pnpm/@storybook+core-server@7.6.12/node_modules/@storybook/core-server/dist/index.js:120:1397)
    at async withTelemetry (/Users/${username}/monorepo-sample/node_modules/.pnpm/@storybook+core-server@7.6.12/node_modules/@storybook/core-server/dist/index.js:65:3948)
    at async dev (/Users/${username}/monorepo-sample/node_modules/.pnpm/@storybook+cli@7.6.12/node_modules/@storybook/cli/dist/generate.js:514:401)
    at async Command.<anonymous> (/Users/${username}/monorepo-sample/node_modules/.pnpm/@storybook+cli@7.6.12/node_modules/@storybook/cli/dist/generate.js:529:225)
```



### System

```bash
System:
    OS: macOS 13.5
    CPU: (12) arm64 Apple M2 Pro
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.19.0 - ~/Library/pnpm/node
    npm: 10.2.3 - ~/Library/pnpm/npm
    pnpm: 8.15.1 - ~/Library/pnpm/pnpm <----- active
  Browsers:
    Chrome: 121.0.6167.139
    Safari: 16.6
```


### Additional context

_No response_