docusaurus: Page crashes when trying to use live code block
Have you read the Contributing Guidelines on issues?
- I have read the Contributing Guidelines on issues.
Prerequisites
- I’m using the latest version of Docusaurus.
- I have tried the
npm run clearoryarn clearcommand. - I have tried
rm -rf node_modules yarn.lock package-lock.jsonand re-installing packages. - I have tried creating a repro with https://new.docusaurus.io.
- I have read the console error message carefully (if applicable).
Description
When trying to use live code blocks, I get this error (rendered on the client, not in terminal):
This page crashed. “useColorMode()” is used outside of “Layout” component. Please see https://docusaurus.io/docs/api/themes/configuration#use-color-mode.
Note that I get no such error when just using code blocks. The error appears when I add live after the language meta tag.
I’m using version 2.0.0-beta.15.
Steps to reproduce
To any Markdown docs page, add a live code block, like this example from the documentation:
```jsx live
function Clock(props) {
const [date, setDate] = useState(new Date());
useEffect(() => {
var timerID = setInterval(() => tick(), 1000);
return function cleanup() {
clearInterval(timerID);
};
});
function tick() {
setDate(new Date());
}
return (
<div>
<h2>It is {date.toLocaleTimeString()}.</h2>
</div>
);
}
```
This should produce the error described above. Now, remove live after the language meta tag. This should result in no error.
Expected behavior
The live code block should work without error.
Actual behavior
I get the error described above.
Your environment
- Public source code:
- Public site URL:
- Docusaurus version used: 2.0.0-beta.15
- Environment name and version (e.g. Chrome 89, Node.js 16.4):
- Operating system and version (e.g. Ubuntu 20.04.2 LTS):
Reproducible demo
No response
Self-service
- I’d be willing to fix this bug myself.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 1
- Comments: 16
Found a solution for those that are running into this bug with
pnpm. Essentially issue ispnpminstalls 2 instances (but same version and sha256 hash!) of@docusaurus/theme-commonthis causes a mismatch in theColorProvidercontext anduseColorProviderinvoked by livecodePlayground.Current albeit a bit hack solution I found was to just ignore all dependencies on
@docusaurus/*packages that are already explicitly included in my project’s dependencies (and are therefore listed in the package.json).Also useful is hoisting the relevant libraries:
EDIT: As mentioned from below answers, I forgot to mention you have to also add
@docusaurus/theme-commonto your package.json and also delete any pnpm-lock.yaml file (pnpmfile.cjs is only used when resolving depedencies, therefore it get’s bypassed if you have a lockfile).Was running into the same issue. I tested with the official docusaurus starter.
npmand it worked.node_modules, installed withpnpmbrokenHook useColorMode is called outside the <ColorModeProvider>errorThis leads me to believe it is an issue with webpack5 & pnpm.
I’m also using pnpm and the above solution didn’t work. I fixed it by adding
@docusaurus/theme-commonin the package.json of my doc package in my monorepo@certainlyNotHeisenberg Since it succeeds in the Docusaurus site, I can guarantee that it is not a Docusaurus bug. Do you have a repro? Have you swizzled any component? Please don’t use the GitHub issue tracker as a support forum—unless you can clearly outline how it’s a Docusaurus defect, we will treat it as a question.
In my case, I actually had to add
@docusaurus/typesto thedependenciesof my doc project -@docusaurus/theme-commonwas being duplicated by PNPM because@docusaurus/utilssometimes had the@docusaurus/typesoptional dependency provided and sometimes not. By providing it explicitly in my dependencies, it eliminated the duplicates and fixed my problem.Many thanks. I tried below steps and it works as magic. Also thanks to https://github.com/facebook/docusaurus/issues/7880.
Solution 1:
Hoist doc’s
@docusaurus/theme-commoninto rootnode_modules:Root’s
.npmrc:Then delete root’s & doc’s
node_modulesfolders and reinstall.Solution 2:
Move
@docusaurus/theme-commonintopeerDependencies:Doc’s
package.json:The only case that I can think of to make it fail in code sandbox without non-trivial effort is exporting a page from
src/pageswithout wrapping it inLayout. Because any Markdown page should always be wrapped withLayout