question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Getting invalid hooks error when loading markdown outside of docusaurs's root folder

See original GitHub issue

Have you read the Contributing Guidelines on issues?

Prerequisites

  • I’m using the latest version of Docusaurus.
  • I have tried the npm run clear or yarn clear command.
  • I have tried rm -rf node_modules yarn.lock package-lock.json and 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

I don’t think it is a Docusaurus problem itself but I’m missing some knowledge on internals to debug my situation.

When trying to import a file outside of the docusaurus folder in a monorepo like

/```mdx-code-block 
import Contributingfrom "@site/../CONTRIBUTING.md"

<Contributing />
/```

I’m getting the basic invalid hook error

Screenshot 2022-09-13 at 10 41 58

It is coming from the useMDXComponents hooks apparently.

So I figured it was a dependency conflict with my monorepo. I can confirm it is a dependency problem by removing the packages to test this.

All my react/react-dom versions are identical, so it looks like it is a problem with react being loaded twice problem.

Since I’m only loading a plain markdown file, I don’t understand why that would start conflicting. Do you have some pointers/recommendations to fix this?

Reproducible demo

No response

Steps to reproduce

I couldn’t pinpoint the exact problem sadly and need some insights about how docusaurus might be import things

Expected behavior

No hooks error when loading md files outside of docusaurus folder

Actual behavior

Getting the invalid hook error.

Your environment

  • Docusaurus version used:2.1.0
  • Environment name and version (e.g. Chrome 89, Node.js 16.4): node 18
  • Operating system and version (e.g. Ubuntu 20.04.2 LTS): mac os monterey

Self-service

  • I’d be willing to fix this bug myself.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
alexandrebodincommented, Nov 8, 2022

The proposed webpack config worked like a charm. Thanks for the tip 👍

I’m going to go ahead and close this issue now. Thank you for your help

1reaction
pdilloncommented, Oct 5, 2022

I ran into a similar issue, but not with mdx. Not sure if this would work for you, but adding this to plugins in docusaurus.config.js worked for me:

 plugins: [
    () => ({
      name: 'resolve-react',
      configureWebpack() {
        return {
          resolve: {
            alias: {
              // assuming root node_modules is up from "./packages/<your-docusaurus>
              react: path.resolve('../../node_modules/react'), 
            },
          },
        };
      },
    }),
    // ... other plugins
Read more comments on GitHub >

github_iconTop Results From Across the Web

docusaurus.config.js
docusaurus.config.js contains configurations for your site and is placed in the root directory of your site. It usually exports a site ...
Read more >
Invalid Hook Call Warning
Invalid Hook Call Warning ... You are probably here because you got the following error message: Hooks can only be called inside the...
Read more >
Invalid hook call. Hooks can only be called inside of the ...
I had this issue when I used npm link to install my local library, which I've built using cra . I found the...
Read more >
@docusaurus/plugin-debug | Yarn - Package Manager
We are working hard on Docusaurus v2. If you are new to Docusaurus, try using the new version instead of v1. See the...
Read more >
markdown
logLevel will give you a bit more visibility into any errors that may arise. ... a folder structure similar to what's shown below,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found