asyncapi-react: Can't import in a Docusaurus project - error `can't resolve 'http'`

Description

My goal is to deploy two docs (OpenAPI & AsyncAPI) together in the same place.

  1. I generate a Docusaurus website using steps docusaurus-openapi. The result is at the “Initial commit” of the reproducible minimal example.

  2. I add a custom react page as described in their doc and import a random react component in it (‘awesome-react-button’), and it works:

image
  1. I import the AsyncAPI React component (exact diff commit) but now it crashes:
image

Expected result

Import to work and not break everything.

Actual result

Compilation errors

Full errors snippet
Module not found: Error: Can't resolve 'http' in '/Users/achraf/exp_docs/docusaurus/my-website/node_modules/@apidevtools/json-schema-ref-parser/lib/resolvers'
Did you mean './http'?
Requests that should resolve in the current directory need to start with './'.
Requests that start with a name are treated as module requests and resolve within module directories (/Users/achraf/exp_docs/docusaurus/my-website/node_modules/@docusaurus/core/node_modules, node_modules, /Users/achraf/exp_docs/docusaurus/my-website/node_modules).
If changing the source code is not an option there is also a resolve options called 'preferRelative' which tries to resolve these kind of requests in the current directory too.

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
        - add a fallback 'resolve.fallback: { "http": require.resolve("stream-http") }'
        - install 'stream-http'
If you don't want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { "http": false }
Module not found: Error: Can't resolve 'https' in '/Users/achraf/exp_docs/docusaurus/my-website/node_modules/@apidevtools/json-schema-ref-parser/lib/resolvers'
BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
        - add a fallback 'resolve.fallback: { "https": require.resolve("https-browserify") }'
        - install 'https-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { "https": false }
Module not found: Error: Can't resolve 'stream' in '/Users/achraf/exp_docs/docusaurus/my-website/node_modules/avsc/etc/browser'
BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
        - add a fallback 'resolve.fallback: { "stream": require.resolve("stream-browserify") }'
        - install 'stream-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { "stream": false }
Module not found: Error: Can't resolve 'stream' in '/Users/achraf/exp_docs/docusaurus/my-website/node_modules/avsc/lib'
BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
        - add a fallback 'resolve.fallback: { "stream": require.resolve("stream-browserify") }'
        - install 'stream-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { "stream": false }
Module not found: Error: Can't resolve 'zlib' in '/Users/achraf/exp_docs/docusaurus/my-website/node_modules/avsc/lib'
BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
        - add a fallback 'resolve.fallback: { "zlib": require.resolve("browserify-zlib") }'
        - install 'browserify-zlib'
If you don't want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { "zlib": false }
Module not found: Error: Can't resolve 'stream' in '/Users/achraf/exp_docs/docusaurus/my-website/node_modules/avsc/lib'
BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
        - add a fallback 'resolve.fallback: { "stream": require.resolve("stream-browserify") }'
        - install 'stream-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { "stream": false }
client (webpack 5.88.0) compiled with 6 errors

Steps to reproduce

Checkout before/after this commit in the reproducible: https://github.com/AchrafAmil/DocusaurusAsyncAPI/commit/7370bc343020ff4a6632fa72edc58b9474e63bab

Troubleshooting

Web dev is really not my comfort zone so I might be doing/saying something wrong, but it seems like the AsyncAPI React component is using a node.js dependency that shouldn’t be there (?).

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 17 (7 by maintainers)

Most upvoted comments

@AchrafAmil another workaround, until a new version of the AsyncApiComponent is released (next branch), is by using the Standalone Bundle loaded in the useEffect hook of a custom component:

function AsyncApiBrowserStandalone({ schema, config }: ContainerProps) {
  useEffect(() => {
    const AsyncApiStandalone = require('@asyncapi/react-component/browser/standalone');

    AsyncApiStandalone.render(
      {
        schema: schema,
        config: config,
      },
      document.getElementById('asyncapi'),
    );

  }, [schema, config]);

  return <div id="asyncapi">Loading...</div>;
}

You may also load the standalone script in the scripts section of the docusaurus.config.js, if so, remeber to also manually load the stylesheet.

oh not cool 😞

you have 2 options:

  • wait once we migrate to use in react the new v2 parser that uses different package that do not use http dependency. I cannot commit to any dates. Unless @fmvilas planned something already
  • you can try workaround this problem with https://www.npmjs.com/package/module-alias if you are in rush. Below you see chatgpt hint that sounds like it could work Screenshot 2023-06-29 at 11.12.37.png

FWIW, I went for the (pretty savage but quick) workaround of simply putting the fat single-file html+css+javascript in a React component. It gives the following results: https://docs.nabla.com/server/copilot-listen-ws