Skip to content

unist-util-visit: Cannot use 'in' operator to search for 'children' in undefined #14211

@mark-wiemer

Description

@mark-wiemer

Astro Info

Astro                    v5.12.9
Node                     v22.17.1
System                   Windows (x64)
Package Manager          npm
Output                   static
Adapter                  none
Integrations             @astrojs/starlight

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

  1. Created a new project with npm create astro@latest -- --template starlight

  2. Added unist-util-visit via npm i unist-util-visit

  3. Updated dependencies to latest:

    "dependencies": {
      "@astrojs/starlight": "^0.35.2",
      "astro": "^5.12.9",
      "sharp": "^0.34.3",
      "unist-util-visit": "^5.0.0"
    }
    
  4. Added custom rewriteLink func to remarkPlugins:

     const rewriteLinks = (base) => {
       return (tree) => {
         visit(tree, "link", (node) => {
           if (node.url.startsWith("./") || node.url.startsWith("../")) {
             // Remove leading ./ or ../ and prepend base
             node.url = base + "/" + node.url.replace(/^(\.\/|\.\.\/)+/, "");
           }
         });
       };
     };
     const base = "/next";
     // ...
     export default defineConfig({
       // ...
       markdown: {
         remarkPlugins: [rewriteLinks(base)],
       },
     })
  5. Ran npm run dev

  6. Opened page and got an error:

MDXError
An error occurred.
Cannot use 'in' operator to search for 'children' in undefined

Some verbose logs:

  vite:transform 2.97ms /.astro/content-modules.mjs +16ms
  vite:load 1.60ms [fs] /src/content/docs/index.mdx?astroPropagatedAssets +6ms       
  vite:load 0.49ms [fs] src/content/docs/index.mdx +1ms
  astro:vite Error when evaluating SSR module C:/Users/markw/my-stuff/mocha-stuff/hello-astro/src/content/docs/index.mdx: Cannot use 'in' operator to search for 'children' in undefined
  astro:vite   Plugin: @mdx-js/rollup
  astro:vite   File: C:/Users/markw/my-stuff/mocha-stuff/hello-astro/src/content/docs/index.mdx:undefined:undefined
  astro:vite       at TransformPluginContext.transform (file:///C:/Users/markw/my-stuff/mocha-stuff/hello-astro/node_modules/@astrojs/mdx/dist/vite-plugin-mdx.js:62:15)  
  astro:vite       at EnvironmentPluginContainer.transform (file:///C:/Users/markw/my-stuff/mocha-stuff/hello-astro/node_modules/vite/dist/node/chunks/dep-DBxKXgDP.js:42295:19)
  astro:vite       at async loadAndTransform (file:///C:/Users/markw/my-stuff/mocha-stuff/hello-astro/node_modules/vite/dist/node/chunks/dep-DBxKXgDP.js:35735:27) +0ms   
17:54:47 [ERROR] Cannot use 'in' operator to search for 'children' in undefined      
  Location:
    C:/Users/markw/my-stuff/mocha-stuff/hello-astro/src/content/docs/index.mdx:0:0   
  Stack trace:
    at TransformPluginContext.transform (file:///C:/Users/markw/my-stuff/mocha-stuff/hello-astro/node_modules/@astrojs/mdx/dist/vite-plugin-mdx.js:62:15)
    at async loadAndTransform (file:///C:/Users/markw/my-stuff/mocha-stuff/hello-astro/node_modules/vite/dist/node/chunks/dep-DBxKXgDP.js:35735:27)
  vite:time 1997.77ms / +0ms
  vite:resolve 0.39ms /@vite/client -> C:/Users/markw/my-stuff/mocha-stuff/hello-astro/node_modules/vite/dist/client/client.mjs +64ms
  vite:load 1.43ms [fs] /@vite/client +61ms
  vite:resolve 0.12ms @vite/env -> C:/Users/markw/my-stuff/mocha-stuff/hello-astro/node_modules/vite/dist/client/env.mjs +17ms
  vite:import-analysis 1.24ms [1 imports rewritten] node_modules/vite/dist/client/client.mjs +81ms
  vite:transform 15.62ms /@vite/client +81ms
  vite:time 23.17ms /@vite/client +70ms
  vite:load 6.94ms [fs] /node_modules/vite/dist/client/env.mjs +23ms
  vite:import-analysis 0.04ms [no imports] node_modules/vite/dist/client/env.mjs +7ms
  vite:transform 0.60ms /node_modules/vite/dist/client/env.mjs +8ms
  vite:cache [304] /node_modules/vite/dist/client/env.mjs +0ms
  vite:time 0.32ms /node_modules/vite/dist/client/env.mjs +11ms

What's the expected result?

Page should load with any relative URLs rewritten. I know rewriteLinks isn't perfect but it shouldn't cause an MDXError

Link to Minimal Reproducible Example

https://github.com/mark-wiemer/hello-astro/tree/mocha-issue-5415

Participation

  • I am willing to submit a pull request for this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs triageIssue needs to be triaged

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions