The previous implementation supported relative references to packages
(e.g. `next/dist/compiled/react-dom`)
and named exports
(e.g. `next/dist/compiled/react-dom/client`),
but not references to physical script files within packages
(e.g. `next/dist/compiled/react-dom/cjs/react-dom-test-utils.production.js`).
The latter are now handled by detecting when require.resolve returns a path and filename that ends with the exact string provided to the function.
In a pnpm monorepo with multiple Next.js projects, different projects can have different peerDependencies for Next.js, which causes pnpm to install multiple instances of the `next` package in the monorepo. In this situation, prior to this change, Storybook's webpack configuration could consume `react` from one instance of Next and `react-dom` from another, causing React render errors due to the mismatch.
This issue was caused by the `configureConfig` function adding webpack module aliases for `react` using `addScopedAlias` (which generates an alias with an absolute filesystem path pointing to the correct instance of `next`, e.g. `'/path/to/next/dist/compiled/react'`), but adding module aliases for `react-dom` using `setAlias` (which generates an alias with a relative path, e.g. `'next/dist/compiled/react-dom'`).
This would create a webpack configuration like this:
```
alias: {
⋮
react: '/Users/kyank/Developer/authentication-ui/node_modules/.pnpm/next@14.2.13_@babel+core@7.25.2_@opentelemetry+api@1.8.0_@playwright+test@1.48.1_babel-plugin_z4uy3ayinaafvek4wmyon66ziu/node_modules/next/dist/compiled/react',
⋮
'react-dom/test-utils': 'next/dist/compiled/react-dom/cjs/react-dom-test-utils.production.js',
'react-dom$': 'next/dist/compiled/react-dom',
'react-dom/client': 'next/dist/compiled/react-dom/client',
'react-dom/server': 'next/dist/compiled/react-dom/server',
⋮
},
```
This change uses `addScopedAlias` to create aliases with absolute filesystem paths for all of the Next.js-bundled React packages. This fixes the React rendering errors in our monorepo.
This issue seems to have been [introduced in Storybook 8.3.0](https://github.com/storybookjs/storybook/pull/29044/files#diff-20144c44999f6f1054f74f56ef1c3fcfcec008fd7b5caea5e10568e95eccb051).