next-plugins icon indicating copy to clipboard operation
next-plugins copied to clipboard

Is next-mdx broken in Next.js v8?

Open starman-vlad opened this issue 7 years ago • 6 comments

I'm trying to parse a .md file but it doesn't work no matter what I try.

component:

import glossary from '../markdown/glossary/glossary.md'

export default () => (
    <div>{glossary}</div>
)

next.config.js

const withPlugins = require('next-compose-plugins')
const withCSS = require('@zeit/next-css')
const withMDX = require('@zeit/next-mdx')({
    extension: /\.(md|mdx)$/
})

module.exports = withPlugins([
    withMDX,
    withCSS
], {
    target: 'serverless'
})

And the error is

Warning: Functions are not valid as a React child. This may happen if you return a Component instead of <Component /> from render. Or maybe you meant to call this function rather than return it.
    in div (at glossary.jsx:4)
    in Unknown (created by App)
    in Container (created by App)
    in App

I even tried exporting like this

module.exports = withPlugins([
    withMDX(),
    withCSS()
], {
    target: 'serverless'
})

and then a different error occurs

./markdown/glossary/glossary.md 1:0
Module parse failed: Unexpected character '#' (1:0)
You may need an appropriate loader to handle this file type.
> # Reactive Manifesto Glossary
| 
| * [Asynchronous](#Asynchronous)

Can someone tell me what exactly is the issue? Because I believe I'm extending the webpack config properly, just don't understand why it won't parse.

starman-vlad avatar Feb 13 '19 13:02 starman-vlad

Ran into the same thing. This works:

{
  target: 'serverless',
  webpack(c) {
    c.module.rules.push({
      test: /\.mdx?$/,
      use: ['babel-loader', '@mdx-js/loader']
    })

    return c
  }
}

OutThisLife avatar May 12 '19 20:05 OutThisLife

Same issue here, not solved 👎

rdewolff avatar Aug 28 '19 14:08 rdewolff

Here is working on next.js v9.1 with:

next.config.js

const withPlugins = require('next-compose-plugins');
const withCSS = require('@zeit/next-css');
const withMDX = require('@next/mdx')({
  extension: /\.(md|mdx)$/,
});

module.exports = withPlugins([
  withMDX({
    pageExtensions: ['js', 'jsx', 'mdx', 'md'],
  }),
  withCSS
]);

/* without 'withPlugins'
module.exports = withCSS(
  withMDX({
    pageExtensions: ['js', 'jsx', 'mdx', 'md'],
  })
);
*/

  • imported .md file is a React Component
  • .md files must be imported from a page file. It will not work from a children component (follow the same rules as getInitialProps)

pages/index.js

import Readme from '../doc/readme.md';

export default () => <Readme />;

bySabi avatar Nov 28 '19 19:11 bySabi

~~@bySabi can we get some version info other than next? I am using all latest and this does not work. I copied your exact configuration~~

Module parse failed: Unexpected character '#' (2:0) You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders

~~In my case I have;~~

~~1. Created a page~~ ~~2. Imported a md file to that page~~

~~I am not trying to generate pages from mdx~~

~~Might also be worth noting that I tested with next@^9.1 and latest @next/mdx -- no difference in behaviour.~~

Scratch this -- It works perfectly. frekkin Mac space sneaked itself inside next.config.js´s filename.

abdavid avatar Feb 03 '20 20:02 abdavid

Hey! We're deprecating this repository, next-mdx has been moved to https://npmjs.com/@next/mdx

timneutkens avatar Dec 18 '20 07:12 timneutkens

@timneutkens Thanks. It could've been an issue with my code syntax itself, since I now see that I tried to import the markdown file like this

import glossary from '../markdown/glossary/glossary.md'

export default () => (
    <div>{glossary}</div>
)

which is probably not the proper way to import the markdown file, but rather it should be imported like this

import Glossary from '../markdown/glossary/glossary.md'
// or
import glossary from '../markdown/glossary/glossary.md'

export default () => (
    <Glossary />
    // or
    <div dangerouslySetInnerHTML={{ __html: glossary }} />
)

Although I remember trying a lot of different methods to import the markdown file without any success, but thanks for letting us know that the repository is now deprecated.

starman-vlad avatar Dec 18 '20 14:12 starman-vlad