content-collections icon indicating copy to clipboard operation
content-collections copied to clipboard

Mermaid plugin error

Open Bartek532 opened this issue 10 months ago • 9 comments

Hi, I'm trying to add this remark plugin: https://www.npmjs.com/package/@theguild/remark-mermaid to parse Mermaid syntax, but when I try to build my content collections it throws errors:

build started ...
✘ [ERROR] Could not resolve "tty"

    ../../node_modules/debug/src/node.js:5:20:
      5 │ const tty = require('tty');
        ╵                     ~~~~~

  The package "tty" wasn't found on the file system but is built into node. Are you trying to bundle
  for node? You can use "platform: 'node'" to do that, which will remove this error.

✘ [ERROR] Could not resolve "util"

    ../../node_modules/debug/src/node.js:6:21:
      6 │ const util = require('util');
        ╵                      ~~~~~~

  The package "util" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

✘ [ERROR] Could not resolve "os"

    ../../node_modules/supports-color/index.js:2:19:
      2 │ const os = require('os');
        ╵                    ~~~~

  The package "os" wasn't found on the file system but is built into node. Are you trying to bundle
  for node? You can use "platform: 'node'" to do that, which will remove this error.

✘ [ERROR] Could not resolve "tty"

    ../../node_modules/supports-color/index.js:3:20:
      3 │ const tty = require('tty');
        ╵                     ~~~~~

  The package "tty" wasn't found on the file system but is built into node. Are you trying to bundle
  for node? You can use "platform: 'node'" to do that, which will remove this error.

My schema is extremely simple:

const blog = defineCollection({
  name: "blog",
  directory: "src/content/blog",
  include: "**/*.mdx",
  schema: (z) => ({
    title: z.string(),
    description: z.string(),
  }),
  transform: async (doc, context) => {
    const mdx = await transformMDX(doc, context, {
      remarkPlugins: [remarkImage, remarkMermaid],
    });

    return mdx;
  },
});

What should I do to fix it?

I'm using Next.js in Turborepo if it matters, but it basically throws the same when I do content-collections build

Bartek532 avatar Feb 10 '25 14:02 Bartek532

Can you provide a repository to reproduce the issue? If i try to reproduce it, i became a totally different issue:

Starting content-collections content-collections.ts
build started ...
✘ [ERROR] Could not resolve "@theguild/remark-mermaid/mermaid"

    _mdx_bundler_entry_point-139798a1-d2e3-4f39-86d9-e52bb1c2b82f.mdx:2:22:
      2 │ import {Mermaid} from "@theguild/remark-mermaid/mermaid";
        ╵                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  You can mark the path "@theguild/remark-mermaid/mermaid" as external to exclude it from the
  bundle, which will remove this error and leave the unresolved path in the bundle.

... error Error: Build failed with 1 error:
_mdx_bundler_entry_point-139798a1-d2e3-4f39-86d9-e52bb1c2b82f.mdx:2:22: ERROR: Could not resolve "@theguild/remark-mermaid/mermaid"

sdorra avatar Feb 11 '25 19:02 sdorra

Ok, i've used a quick workaround to bypass the problem with the missing path:

cp node_modules/@theguild/remark-mermaid/dist/mermaid.js node_modules/@theguild/remark-mermaid/

After that i could use mermaid diagrams in a code block of a mdx file.

sdorra avatar Feb 11 '25 19:02 sdorra

Okay, but would I solve it in a production build? I'll need to do some patch?

Bartek532 avatar Feb 11 '25 19:02 Bartek532

The error went away if I pass the cwd to the compileMDX:

const content = await compileMDX(ctx, post, {
  cwd: process.cwd(),
  remarkPlugins: [remarkMermaid],
});

I'm not sure why it is not the default in mdx-bundler (which is used by @content-collections/mdx).

sdorra avatar Feb 11 '25 20:02 sdorra

@Bartek532, can you confirm that passing the cwd resolves the issue?

sdorra avatar Feb 13 '25 11:02 sdorra

@sdorra hey, no, the issue still persists

> next dev --turbo

Starting content-collections content-collections.ts
build started ...
✘ [ERROR] Could not resolve "tty"

    ../../node_modules/debug/src/node.js:5:20:
      5 │ const tty = require('tty');
        ╵                     ~~~~~

  The package "tty" wasn't found on the file system but is built into node. Are you trying to bundle
  for node? You can use "platform: 'node'" to do that, which will remove this error.

✘ [ERROR] Could not resolve "util"

    ../../node_modules/debug/src/node.js:6:21:
      6 │ const util = require('util');
        ╵                      ~~~~~~

  The package "util" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

✘ [ERROR] Could not resolve "os"

    ../../node_modules/supports-color/index.js:2:19:
      2 │ const os = require('os');
        ╵                    ~~~~

  The package "os" wasn't found on the file system but is built into node. Are you trying to bundle
  for node? You can use "platform: 'node'" to do that, which will remove this error.

✘ [ERROR] Could not resolve "tty"

    ../../node_modules/supports-color/index.js:3:20:
      3 │ const tty = require('tty');
        ╵                     ~~~~~

  The package "tty" wasn't found on the file system but is built into node. Are you trying to bundle
  for node? You can use "platform: 'node'" to do that, which will remove this error.

... error Error: Build failed with 4 errors:
../../node_modules/debug/src/node.js:5:20: ERROR: Could not resolve "tty"
../../node_modules/debug/src/node.js:6:21: ERROR: Could not resolve "util"
../../node_modules/supports-color/index.js:2:19: ERROR: Could not resolve "os"
../../node_modules/supports-color/index.js:3:20: ERROR: Could not resolve "tty"

Bartek532 avatar Feb 13 '25 12:02 Bartek532

@Bartek532 i can’t reproduce the issue. Can you please provide a sample repository?

sdorra avatar Feb 14 '25 11:02 sdorra

@sdorra sure, here it is: https://github.com/Bartek532/cc-mermaid-plugin-error - when I run pnpm dev I see the above errors ;)

Bartek532 avatar Feb 14 '25 13:02 Bartek532

Things are also going well for me with this repository:

> [email protected] dev /Users/sdorra/Desktop/cc-mermaid-plugin-error
> next dev --turbopack

Starting content-collections content-collections.ts
build started ...
... finished build of 1 collection and 1 document in 1644ms
start watching ...
   ▲ Next.js 15.1.7 (Turbopack)
   - Local:        http://localhost:3000
   - Network:      http://192.168.1.154:3000

 ✓ Starting...
 ✓ Ready in 712ms
 ○ Compiling / ...
 ✓ Compiled / in 1280ms
 GET / 200 in 1438ms

sdorra avatar Feb 15 '25 12:02 sdorra