storybook: MDX2: Can't resolve '@mdx-js/react'
Describe the bug Trying to integrate storybook@next with CRA and running into a runtime error
To Reproduce Please run ‘npm run storybook’ in my repo: https://github.com/nareshbhatia/cra-with-storybook. You will get this error and Storybook with crash. The commit history explains how the repo was built in 3 easy steps as recommended in the docs.
ModuleNotFoundError: Module not found: Error: Can't resolve '@mdx-js/react' in '/Users/naresh/projects/cra-with-storybook/src/stories'
at /Users/naresh/projects/cra-with-storybook/node_modules/webpack/lib/Compilation.js:2016:28
at /Users/naresh/projects/cra-with-storybook/node_modules/webpack/lib/NormalModuleFactory.js:798:13
at eval (eval at create (/Users/naresh/projects/cra-with-storybook/node_modules/webpack/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:10:1)
at /Users/naresh/projects/cra-with-storybook/node_modules/webpack/lib/NormalModuleFactory.js:270:22
at eval (eval at create (/Users/naresh/projects/cra-with-storybook/node_modules/webpack/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:9:1)
at /Users/naresh/projects/cra-with-storybook/node_modules/webpack/lib/NormalModuleFactory.js:434:22
at /Users/naresh/projects/cra-with-storybook/node_modules/webpack/lib/NormalModuleFactory.js:116:11
at /Users/naresh/projects/cra-with-storybook/node_modules/webpack/lib/NormalModuleFactory.js:670:25
at /Users/naresh/projects/cra-with-storybook/node_modules/webpack/lib/NormalModuleFactory.js:855:8
at /Users/naresh/projects/cra-with-storybook/node_modules/webpack/lib/NormalModuleFactory.js:975:5
at /Users/naresh/projects/cra-with-storybook/node_modules/neo-async/async.js:6883:13
at /Users/naresh/projects/cra-with-storybook/node_modules/webpack/lib/NormalModuleFactory.js:958:45
at finishWithoutResolve (/Users/naresh/projects/cra-with-storybook/node_modules/enhanced-resolve/lib/Resolver.js:312:11)
at /Users/naresh/projects/cra-with-storybook/node_modules/enhanced-resolve/lib/Resolver.js:386:15
at /Users/naresh/projects/cra-with-storybook/node_modules/enhanced-resolve/lib/Resolver.js:435:5
at eval (eval at create (/Users/naresh/projects/cra-with-storybook/node_modules/enhanced-resolve/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:16:1)
resolve '@mdx-js/react' in '/Users/naresh/projects/cra-with-storybook/src/stories'
Parsed request is a module
using description file: /Users/naresh/projects/cra-with-storybook/package.json (relative path: ./src/stories)
Field 'browser' doesn't contain a valid alias configuration
resolve as module
/Users/naresh/projects/cra-with-storybook/src/stories/node_modules doesn't exist or is not a directory
/Users/naresh/projects/cra-with-storybook/src/node_modules doesn't exist or is not a directory
looking for modules in /Users/naresh/projects/cra-with-storybook/node_modules
single file module
using description file: /Users/naresh/projects/cra-with-storybook/package.json (relative path: ./node_modules/@mdx-js/react)
no extension
Field 'browser' doesn't contain a valid alias configuration
/Users/naresh/projects/cra-with-storybook/node_modules/@mdx-js/react doesn't exist
.mjs
Field 'browser' doesn't contain a valid alias configuration
/Users/naresh/projects/cra-with-storybook/node_modules/@mdx-js/react.mjs doesn't exist
.js
Field 'browser' doesn't contain a valid alias configuration
/Users/naresh/projects/cra-with-storybook/node_modules/@mdx-js/react.js doesn't exist
.jsx
Field 'browser' doesn't contain a valid alias configuration
/Users/naresh/projects/cra-with-storybook/node_modules/@mdx-js/react.jsx doesn't exist
.ts
Field 'browser' doesn't contain a valid alias configuration
/Users/naresh/projects/cra-with-storybook/node_modules/@mdx-js/react.ts doesn't exist
.tsx
Field 'browser' doesn't contain a valid alias configuration
/Users/naresh/projects/cra-with-storybook/node_modules/@mdx-js/react.tsx doesn't exist
.json
Field 'browser' doesn't contain a valid alias configuration
/Users/naresh/projects/cra-with-storybook/node_modules/@mdx-js/react.json doesn't exist
.cjs
Field 'browser' doesn't contain a valid alias configuration
/Users/naresh/projects/cra-with-storybook/node_modules/@mdx-js/react.cjs doesn't exist
/Users/naresh/projects/cra-with-storybook/node_modules/@mdx-js/react doesn't exist
/Users/naresh/projects/node_modules doesn't exist or is not a directory
/Users/naresh/node_modules doesn't exist or is not a directory
/Users/node_modules doesn't exist or is not a directory
/node_modules doesn't exist or is not a directory
WARN Broken build, fix the error above.
WARN You may need to refresh the browser.
System
Please paste the results of npx sb@next info here.
Environment Info:
System:
OS: macOS 12.3.1
CPU: (4) x64 Intel(R) Core(TM) i5-7600K CPU @ 3.80GHz
Binaries:
Node: 16.13.2 - ~/.nvm/versions/node/v16.13.2/bin/node
Yarn: 1.22.15 - ~/.yarn/bin/yarn
npm: 8.4.0 - ~/.nvm/versions/node/v16.13.2/bin/npm
Browsers:
Chrome: 100.0.4896.127
Safari: 15.4
npmPackages:
@storybook/addon-actions: next => 6.5.0-beta.1
@storybook/addon-essentials: next => 6.5.0-beta.1
@storybook/addon-interactions: next => 6.5.0-beta.1
@storybook/addon-links: next => 6.5.0-beta.1
@storybook/builder-webpack5: next => 6.5.0-beta.1
@storybook/manager-webpack5: next => 6.5.0-beta.1
@storybook/react: next => 6.5.0-beta.1
@storybook/testing-library: ^0.0.11 => 0.0.11
Additional context
Cross reference discussion here: https://github.com/storybookjs/storybook/pull/17215
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 24
- Comments: 37 (6 by maintainers)
Commits related to this issue
- fix storybook dependency issue: https://github.com/storybookjs/storybook/issues/18094#issuecomment-1157525537 — committed to nydelic/nydelic-finances by josias-r 2 years ago
- fix: resolve mdx-js/react module not found issue for Storybook as per https://github.com/storybookjs/storybook/issues/18094#issuecomment-1134326831 — committed to fortoolabs/prototype-01 by vidbina 2 years ago
- chore: set previewMdx2 feature for Storybook as per https://github.com/storybookjs/storybook/issues/18094#issuecomment-1134326831 — committed to fortoolabs/prototype-01 by vidbina 2 years ago
- Update storybook Note: 2 extra things were added in package.json that shouldn't be necessary: * @mdx-js/react@1.6.22 dependency * overrides for the deps of @mdx-js/react This is to avoid a bug in t... — committed to eurofurence/reg-component-library by angryzor 2 years ago
- Switch to mdx2 and add mdx-js/react workaround See https://github.com/storybookjs/storybook/issues/18094#issuecomment-1134326831 — committed to ni/nimble by rajsite a year ago
- Bug 1805573 - make it possible to write .stories.md or .stories.mdx documentation r=mstriemer,tgiles This patch both fixes the dependency issues that we were running into when writing `.stories.mdx` ... — committed to mozilla/gecko-dev by hannajones a year ago
- Bug 1805573 - make it possible to write .stories.md or .stories.mdx documentation r=mstriemer,tgiles This patch both fixes the dependency issues that we were running into when writing `.stories.mdx` ... — committed to jamienicol/gecko by hannajones a year ago
I tried the MDX2 work-around from https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#opt-in-mdx2-support, but I had to install
@mdx-js/reactv2 before it started to work.Here’s the very-slightly modified instructions from the above url.
Before I tried the work-around I got various errors (including the errors listed in this issue and from #18266) depending on what I tried (nothing worked). Storybook would either fail to build or build successfully but render blank pages instead of the MDX content.
And with the work-around, Storybook builds and renders MDX files.
@nareshbhatia
I did some investigations and the issue is, that currently per default
@mdx-js/react@1.x.xversion is used, which definesreactin its peerDependency section (https://github.com/mdx-js/mdx/blob/v1.6.22/packages/react/package.json#L35). React 18 is not part of the allowed version range and therefore@mdx-js/reactwill not be hoisted to the root node_modules folder. Therefore it cannot be resolved by node atm.As I have read the code and some issues,
@mdx-js@2.x.xand therefore@mdx-js/react@2.x.x(which allows to use React 18) will be the default in Storybook 7. You can opt-in for mdx2 like described here: https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#opt-in-mdx2-support (which didn’t solve the issue for me).As a workaround, you could install
@mdx-js/reactand ignore all peerDependency errors thrown by npm 7 by using the flag--legacy-peer-deps. This will install the dependency like npm 4 - npm 6 did it:At least for me, this solved the issue. I don’t know, whether we can bypass this npm 7 exclusive issue until mdx-js@2 deps are used. @shilman any ideas?
This was annoying. Thanks @mrmartineau – you reminded me that pinning the version during install is required.
npm i -D @mdx-js/react@1.6.22npm ciI found a cleaner way to avoid overriding
@mdx-js/react(and let it remain on 1.6.22). The main issue I found was that many dependencies were trying to load react v17 which was conflicting with CRA 5 wanting to load react v18. So my solution was to override react as shown below (note that it was also necessary to overridereact-refreshdue to another problem):I have 2 sample repos demonstrating this solution, one with
preset-create-react-appand another without:Just to add a follow up here, I did try to use the latest suggestion here and use the mdx v2 preview. While the errors are gone now, the weird issue is that the mardown section of the docs does not have any font or theming for some reason. It’s as if you’re viewing a pure html document.
As of now I have resorted to installing mdx-js/react v1 via legacy-peer-deps but that’s kind of a band aid solution.
Can someone look into this further a bit ? I’m bootstraping a basic project with nx and receive all these problems when I create an mdx document.
Thanks @nareshbhatia , it works for me. I think this is more clean:
npm i @mdx-js/reactsolved this issue for me storybook version: 6.5.9 node version: v18.3.0 npm version: 8.11.0 @mdx-js/react: 2.1.1Thank you @valentinpalkovic & @shilman for helping with this.
For now the
--legacy-peer-depsworkaround works for me.FYI, I tried the opt-in-mdx2-support as described in the migration link, but got the following runtime error:
Please feel free to close this issue if it is tracked elsewhere, or keep it open if this is the place to track it!
The only thing that worked for me was to explicitly add
@mdx-js/reactto the dependencies, but stay on v1 andoverrideits dependency on React (to be installed without--legacy-peer-deps):I was getting this in a Vue project… I don’t know why. I had just installed Storybook as well. All I did was switch branches, go back to my storybook brach, did a
npm ilike a good boy, and ran into this error. So,npm iwithout any other changes in between?I guess I can do a reinstall, but really this should not be possible…
This is also need with the latest release of Remix, see https://github.com/VulcanJS/eurodance-stack I will use this config:
This $ syntax will use the same version as your dependencies so it’s better for future updates.
For me just doing
npm i -D @mdx-js/reactdid the job. Didn’t need to pin the version nor addoverridesto package.jsonJust throwing out one more tip. The same repo that was working for me on MacOS (see https://github.com/storybookjs/storybook/issues/18094#issuecomment-1159781896) did not work today on a Windows machine. The culprit was an older version of npm (I think it had npm 8.3.0). I upgraded to the latest npm (8.14.0) and the exact same repo started working just fine! Mind boggling!
For what it’s worth, I found I had to set
legacy-peer-depswith npm. Absolutely nothing else here worked for me.I.e. either using
--legacy-peer-depsor creating.npmrcwithlegacy-peer-deps=true.Should Storybook not install the depedencies it depends on by having them in its dependencies? I know it sounds kind of obvious, but you know, just putting it out there. Maybe this one was forgotten.
Please provide a real fix, for the temporary solution (@marr works for me https://github.com/storybookjs/storybook/issues/18094#issuecomment-1217135470).
Hi all, I had the same, or very similar when moving our setup (storybook 6.5.9 with vue2, webpack5-builder, mdx@1) to node16. As you might now, this node16 (and npm@8) is much stricter on peer-dependencies.
But I managed to have storybook 6.5.12 with vue2, webpack5-builder and mdx@1, in node16! I had to add these 3 devDependencies:
Hope this helps.
Yeah, I’m getting the above too. Vue project, failing due to a React dependency. Madness.
Same. I’m also getting
export 'mdx' was not found in '@mdx-js/react'Edit Need to add the overrides in package.json and install @mdx-js/react@1.6.22
Oh dear, now I have:
Error: Cannot find module ‘react/package.json’ 🤦🏽♂️
I solved by adding
@mdx-js/reactas a dev dependency of my app. Not ideal because it’s not a react app, but it works. I’m sure that at some point someone will say “WTF? Why is this react thingy in our deps? I’ll remove it” and it will blow in his/her face.@nareshbhatia your sample repo’s
package.jsonperfectly works for me! Thank you!@valentinpalkovic thanks for investigating this!
Can we figure out why MDX2 doesn’t solve the problem and potentially fix that? Then we could say “use MDX2 to get React18 support”. And use
--legacy-peer-depsas a workaround if you’re not ready to upgrade.@nareshbhatia does the
--legacy-peer-depsworkaround solve it for you?