Skip to content

Site fails to build production site with blog post that uses require after upgrading to beta 7 #5718

Closed

Description

@charleskorn

🐛 Bug Report

Prerequisites

Description

(A clear and concise description of what the bug is.)

I have a site that builds without error on beta 6.

After upgrading to beta 7, I'm getting errors while doing a production build with yarn build: ReferenceError: require is not defined. (The full stack trace is below.)

I've narrowed the issue down to this element in a blog post:

<AsciinemaPlayer
  src={
    require("./assets/2021-03-01-shell-tab-completion/recording.cast").default
  }
  width={200}
  height={19}
  preload={true}
  poster="npt:4"
/>

Removing this element causes yarn build to succeed, even though I use the exact same thing in documentation pages (examples below), and those build without issue.

I have no issues when I run yarn start, and the affected blog post behaves as expected.

I have a custom plugin that adds a Webpack rule for .cast files. However, neither the error message nor stack trace suggest any issue with this, and it worked without issue with beta 6:

const plugin = function () {
  return {
    name: "asciinema-player",
    // ... snip ...
    configureWebpack() {
      const { loaders } = utils.getFileLoaderUtils();

      return {
        module: {
          rules: [
            {
              test: /\.cast$/,
              use: [loaders.file({ folder: "asciicasts" })],
            },
          ],
        },
      };
    },
  };
};

Furthermore, replacing the path in the require() call to an image does not fix the issue.

Have you read the Contributing Guidelines on issues?

Yes

Steps to reproduce

Use https://new.docusaurus.io to create a CodeSandbox reproducible demo of the bug.

Happy to do this if this would be helpful - just want to check I'm not doing anything obviously wrong first :)

Expected behavior

The site builds without error for development or production, or tells me what the real issue is.

Actual behavior

yarn start builds the site successfully and the affected blog post behaves correctly in the browswer.
yarn build fails with the following output:

$ yarn build
yarn run v1.22.15
$ docusaurus build

[en] Creating an optimized production build...

✔ Client


✔ Server
  Compiled successfully in 31.63s


✔ Client


● Server █████████████████████████ cache (99%) shutdown IdleFileCachePlugin
 stored

Unable to build website for locale "en".
ReferenceError: require is not defined
    at MDXContent (eval at index (/Users/charleskorn/Repositories/Personal/batect/batect.dev/node_modules/@mdx-js/runtime/dist/cjs.js:208:12), <anonymous>:30:100)
    at processChild (/Users/charleskorn/Repositories/Personal/batect/batect.dev/node_modules/react-dom/cjs/react-dom-server.node.development.js:3353:14)
    at resolve (/Users/charleskorn/Repositories/Personal/batect/batect.dev/node_modules/react-dom/cjs/react-dom-server.node.development.js:3270:5)
    at ReactDOMServerRenderer.render (/Users/charleskorn/Repositories/Personal/batect/batect.dev/node_modules/react-dom/cjs/react-dom-server.node.development.js:3753:22)
    at ReactDOMServerRenderer.read (/Users/charleskorn/Repositories/Personal/batect/batect.dev/node_modules/react-dom/cjs/react-dom-server.node.development.js:3690:29)
    at Object.renderToString (/Users/charleskorn/Repositories/Personal/batect/batect.dev/node_modules/react-dom/cjs/react-dom-server.node.development.js:4298:27)
    at mdxToHtml (/Users/charleskorn/Repositories/Personal/batect/batect.dev/node_modules/@docusaurus/utils/lib/mdxUtils.js:26:29)
    at /Users/charleskorn/Repositories/Personal/batect/batect.dev/node_modules/@docusaurus/plugin-content-blog/lib/blogUtils.js:108:44
    at Array.forEach (<anonymous>)
    at generateBlogFeed (/Users/charleskorn/Repositories/Personal/batect/batect.dev/node_modules/@docusaurus/plugin-content-blog/lib/blogUtils.js:100:15)
    at async Object.postBuild (/Users/charleskorn/Repositories/Personal/batect/batect.dev/node_modules/@docusaurus/plugin-content-blog/lib/index.js:338:26)
    at async /Users/charleskorn/Repositories/Personal/batect/batect.dev/node_modules/@docusaurus/core/lib/commands/build.js:158:9
    at async Promise.all (index 1)
    at async buildLocale (/Users/charleskorn/Repositories/Personal/batect/batect.dev/node_modules/@docusaurus/core/lib/commands/build.js:154:5)
    at async tryToBuildLocale (/Users/charleskorn/Repositories/Personal/batect/batect.dev/node_modules/@docusaurus/core/lib/commands/build.js:32:20)
    at async mapAsyncSequencial (/Users/charleskorn/Repositories/Personal/batect/batect.dev/node_modules/@docusaurus/utils/lib/index.js:264:24)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Commenting out the use of require() in this blog post allows yarn build to succeed, despite the fact that the same pattern is used in other places (eg. this page in the documentation).

Your environment

Reproducible demo

Use https://new.docusaurus.io to create a CodeSandbox reproducible demo of the bug.

(Paste the link to an example repo, including a docusaurus.config.js, and exact instructions to reproduce the issue.)

Happy to do this if this would be helpful - just want to check I'm not doing anything obviously wrong first :)

Activity

  1. added
    bug
    status: needs triage
    on Oct 16, 2021
  2. Josh-Cena commented on Oct 16, 2021

    @Josh-Cena
    Collaborator

    This is a Docusaurus bug. From the stack trace this is related to generateBlogFeed, which, since beta.7, renders the entire blog post, but seems it doesn't use the Webpack infra...

    I'm encountering the same issue after the upgrade, because I used module CSS imports in my blog post, but that showed up as "styles is not defined". I see two solutions: catch these errors and handle them with fail-safe; use an alternative (use the MDX loader to load these MD files and use the output HTML, for example)

    Temporarily, I'm wrapping these dirty code with ```mdx-code-block

  3. zxuqian commented on Oct 17, 2021

    @zxuqian
    Contributor

    Same here

  4. Josh-Cena commented on Oct 17, 2021

    @Josh-Cena
    Collaborator

    @zxuqian Temporarily, you can wrap that code in ```mdx-code-block. These code blocks will be unwrapped by MDX but are seen as code blocks by the feeds generation

  5. zxuqian commented on Oct 17, 2021

    @zxuqian
    Contributor

    @zxuqian Temporarily, you can wrap that code in ```mdx-code-block. These code blocks will be unwrapped by MDX but are seen as code blocks by the feeds generation

    Thanks, but I use require a lot to include images and videos. This would not seem to be a solution for me☹️.

  6. Josh-Cena commented on Oct 17, 2021

    @Josh-Cena
    Collaborator

    Yeah, this definitely needs a fix...

  7. zxuqian commented on Oct 17, 2021

    @zxuqian
    Contributor

    I downgraded docusaurus to beta 6 now. Hope this will be fixed soon.

  8. slorber commented on Oct 19, 2021

    @slorber
    Collaborator

    Thanks for reporting.

    We'll track this issue here: #5664

    As it's likely to be annoying for a lot of users we'll likely ship a temporary solution, like the ability to revert to previous behavior with config

  9. slorber commented on Oct 20, 2021

    @slorber
    Collaborator

    Should be fixed in #5753, please try the latest canary after merge

  10. added
    closed: duplicate
    and removed
    status: needs triage
    on Feb 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugclosed: duplicate

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions