Skip to content
This repository has been archived by the owner on Nov 3, 2019. It is now read-only.

SyntaxError: unknown: Identifier '_frontmatter' has already been declared #354

Closed
derekr opened this issue Apr 12, 2019 · 16 comments
Closed

Comments

@derekr
Copy link

derekr commented Apr 12, 2019

Describe the bug
Parsing any mdx files in src/pages causes this parsing error during gatsby develop.

  SyntaxError: unknown: Identifier '_frontmatter' has already been declared (85:  13)
    83 | MDXContent.isMDXComponent =   true;
    84 |
  > 85 | export const _fron  tmatter = {};
       |              ^
    86 |

To Reproduce
https://codesandbox.io/s/pj1k8r410j

  1. Go to https://codesandbox.io/s/pj1k8r410j
  2. Rebuild container (effectively run gatsby develop
  3. See error in stdout

Expected behavior
Build should succeed and dev server is running.

Screenshots
Screen Shot 2019-04-12 at 12 00 00 PM

@ChristopherBiscardi
Copy link
Owner

MDX 1.0 changed the default wrapper to be React.Fragment and also merged defaultLayouts with wrappers so they are the same thing now. The warning can be solved by using MDXProvider to only pass children through to the fragment.

// gatsby-browser.js
import React from "react"
import { MDXProvider } from "@mdx-js/react"

const components = {
	wrapper: ({children}) => <>{children}</>
}
export const wrapRootElement = ({ element }) => {
  return (
    <MDXProvider components={components}>
      {element}
    </MDXProvider>
  )
}

@ChristopherBiscardi
Copy link
Owner

wait, I think I misread this. already declared is a different issue. I know what's happening, hang on.

@derekr
Copy link
Author

derekr commented Apr 12, 2019

So it's a default/expectation that all projects using gatsby-mdx need to include the wrapper replacement in gatsby-browser.js or is this only if you need custom components?

@derekr
Copy link
Author

derekr commented Apr 12, 2019

Also I just realized removing @mdx-deck/gatsby-theme gets things working. I realize everything just dropped like 2 hours ago, but is the expectation that I can/should be able to use the new mdx-deck gatsby theme and a top-level gatsby-mdx configuration even though the mdx-deck theme uses and configures it as an implementation detail?

@ChristopherBiscardi
Copy link
Owner

@derekr still working out what gatsby-mdx's recommendation should be there. It's possible that we could solve it at the library level instead and not have to tell anyone to do anything.

In the meantime, yeah it's expected that if you run into this issue you'd replace the wrapper in your own project.

@ChristopherBiscardi
Copy link
Owner

Also I just realized removing @mdx-deck/gatsby-theme gets things working

My guess is that this is related to having multiple gatsby-mdx plugins. We could ask @jxnblk to add an option to disable the one in the theme as a short-term fix. In the long-term this needs to be solved on either the Gatsby level (allowing plugins to determine how many instances they are allowed to have and how to merge the options) or on the gatsby-mdx level, if we can figure out how to ensure that multiple instances of gatsby-mdx can coexist.

@jxnblk
Copy link

jxnblk commented Apr 12, 2019

On it 🕵🏻‍♂️jxnblk/mdx-deck#325

@derekr
Copy link
Author

derekr commented Apr 12, 2019

So fast!!! Y'all are amazing. This is like the dream setup now. Confirmed fix on my end.

@ChristopherBiscardi
Copy link
Owner

cool, going to close this here then :)

@brycewray
Copy link

Still seeing this occur. Installed gatsby-plugin-mdx in the default starter and got this message (after gatsby develop) upon adding a single .mdx file:

 ERROR

UNHANDLED REJECTION unknown: Identifier '_frontmatter' has already been declared (105:13)

  103 | MDXContent.isMDXComponent = true;
  104 |
> 105 | export const _frontmatter = {};
      |              ^
  106 |



  SyntaxError: unknown: Identifier '_frontmatter' has already been declared (105:13)
    103 | MDXContent.isMDXComponent = true;
    104 |
  > 105 | export const _frontmatter = {};  m
        |              ^
    106 |

...followed by a host of other messages such as:

  - index.js:6325 Object.raise
    [gatsby_site_css-grid_mdx]/[@babel]/parser/lib/index.js:6325:17

  - index.js:3759 ScopeHandler.checkRedeclarationInScope
    [gatsby_site_css-grid_mdx]/[@babel]/parser/lib/index.js:3759:12

  - index.js:3725 ScopeHandler.declareName
    [gatsby_site_css-grid_mdx]/[@babel]/parser/lib/index.js:3725:12

So what is the official fix now?

@cheshrkat
Copy link

I'm getting that error any time I try to specify a layout in gatsby-config.js. Remove the config and it works, add the config and it blows up.

@kendrick
Copy link

kendrick commented Aug 19, 2019

I'm getting that error any time I try to specify a layout in gatsby-config.js. Remove the config and it works, add the config and it blows up.

Same here… @ChristopherBiscardi, any guidance?

@kendrick
Copy link

Fixed the issue I was having: the gatsby-config I was cribbing from wasn't wrapping defaultLayouts in an options object. Seems to be working fine now.

@ChristopherBiscardi, this project is so helpful—thanks for your work on it and for your involvement with Gatsby in general.

@jlengstorf
Copy link
Contributor

I’m hitting this error now using 1.0.34. I have a theme that uses MDX, and I want the site that implements the theme to also use gatsby-plugin-mdx for unrelated pages.

Is there a pattern available to support this, or should I try to figure something else out?

@kiastorm-zz
Copy link

I'm getting this error also when trying to use 3 different themes that use MDX.

gatsby-theme-notes (official gatsby theme)
gatsby-theme-blog (official gatsby theme)
gatsby-theme-documentation (johno)

Does anyone have a fix or workaround??

@johno
Copy link
Collaborator

johno commented Sep 10, 2019

Right now you can configure the themes to only configure MDX like so:

// gatsby-config.js
module.exports = {
  plugins: [
    {
      resolve: 'gatsby-theme-notes',
      options: {
        mdx: false
      }
    },
    {
      resolve: 'gatsby-theme-blog',
      options: {
        mdx: false
      }
    },
    'gatsby-theme-documentation'
  ]
}

We know this isn't an ideal situation and will be working on making the composition story with gatsby-plugin-mdx better.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants