question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"getStaticProps is not defined" with mdx files

See original GitHub issue

Bug report

I’m trying to use nextjs and mdx to build a simple site. When I export getStaticProps, I get an “undefined” error. It looks like an issue that happens client side.

To Reproduce

I followed the “with-mdx” example to add mdx pages to my application. https://github.com/zeit/next.js/tree/canary/examples/with-mdx

I try to generate static props from the mdx using exports (https://mdxjs.com/getting-started#exports)

// src/pages/index.mdx

# Helloworld

Content is here!

export function getStaticProps() {
    return {
        props: {hello: 'world'}
    }
}
// src/_app.tsx
...

export default function App(app: AppProps) {
  const { Component, pageProps } = app;

  return (<MDXProvider components={components}>
      <pre>{JSON.stringify(pageProps)}</pre>
      <Component {...pageProps} />
    </MDXProvider>
  );
}

I get an undefined error:

ReferenceError: getStaticProps is not defined
Module../src/pages/index.mdx
./src/pages/index.mdx:25
  22 | const layoutProps = {
  23 |   layout,
  24 | hello,
> 25 | getStaticProps
  26 | };
  27 | const MDXLayout = "wrapper"
  28 | export default function MDXContent({

The <pre>{hello: "world"}</pre> appears on my webpage. It looks like this error is client side only, and the code behave as expect on the server.

Screenshot of the full error below.

Expected behavior

I expect to see the props and the content.

Screenshots

Screenshot 2020-04-20 at 18 35 00

System information

  • OS: macOS
  • Version of Next.js: 9.3.4
  • Version of Node.js: 12.14.0
    "@mdx-js/loader": "^1.5.8",
    "@next/mdx": "^9.3.5",
    "cors": "^2.8.5",
    "dotenv": "^8.2.0",
    "isomorphic-unfetch": "^3.0.0",
    "next": "^9.3.4",
    "pg": "^8.0.2",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "swr": "^0.2.0",
    "unfetch": "^4.1.0"

Thanks for supporting this project!

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:12
  • Comments:21 (9 by maintainers)

github_iconTop GitHub Comments

6reactions
aralrocacommented, Nov 23, 2020

I get a similar issue, but with async, after writing:

export async function getStaticProps(){
   return { props: { data: await myData() } }
}

image

Rewriting to:

export const getStaticProps = async () => {
   return { props: { data: await myData() } }
}

Just the same as you:

image

6reactions
silvenoncommented, May 8, 2020

You have to export getStaticProps from the MDX page itself, not layout.

There is one syntax that seems to circumvent this incompatibility between MDX and Next.js:

export { getStaticProps } from 'path/to/module'

# Title

Your MDX content...

But I’d say that it’s somewhat of a bug in MDX that this works because all exports are meant to be passed as props to layout, e.g. export const meta = { ... } is a common pattern, and I’d expect export { meta } from './other-data to behave the same, but it doesn’t.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot import meta data from mdx file in getStaticProps nextjs
You can import metadata like follows. First, we export the metadata from within the .mdx file // in /pages/posts/example.mdx import Layout ...
Read more >
Data Fetching: getStaticProps - Next.js
Learn how to use `getStaticProps` to generate static pages with Next.js. ... preview is true if the page is in the Preview Mode...
Read more >
How to Build Your Own Blog with Next.js and MDX
MDX is more or less like the markdown files we always see in GitHub ... and upgrading of Next.js to fix this problem...
Read more >
Building a Blog With Next.js and MDX | The WebStorm Blog
js app, let's set up Bootstrap in our app for styling. We'll use the Bootstrap CDN for this. Inside the ./pages/_app.js file, we'll...
Read more >
MDX with Next.js: The Easiest Way to Build a Static Content Site
Getting MDX up and running with Next.js has never been easier thanks to Contentlayer: a content toolkit that makes working with content in ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found