Open ghost opened 3 years ago
I also tried to bundle a component which is using
import { useRouter } from "next/router"
receiving following error-messages:
First I thought ChakraUI is the problem, but once I comment out the router it works fine.
In your code example, I don't see a src
provided for the image. I'm able to use next/image
with mdx-bundler
as follows:
<Image
alt={`my image`}
src={`/my-image.png`}
width={2548}
height={1640}
/>
After including next/image
as Image
is the forwarded components.
Yes, It worked after Image
component is forwarded or is added as a global component.
@leerob I'm curious why I can't import Image
in the MDX file directly as the way I did with clsx
or React
.
It works:
import clsx from 'clsx'
import * as React from 'react'
But it doesn't work:
import Image from 'next/image'
As far as I am aware next/image
needs to be supplied as a global so that it comes from the next.js app iinstead of being imported seperately. The same is true for next/link
.
As far as I am aware
next/image
needs to be supplied as a global so that it comes from the next.js app iinstead of being imported seperately. The same is true fornext/link
.
I'm not sure to understand how to do that. I added
globals: { 'next/image': 'nextImage', 'next/link': 'nextLink' }
to the bundleMDX
function and provided
import NextImg from 'next/image';
import Link from 'next/link';
/*
...
*/
getMDXComponent(code, { nextImage: NextImg, nextLink: Link });
to the page component, but still receive this error whenever I import a component using next/image :
ReferenceError: process is not defined
Am I missing something here ?
I was encountering a similar issue. It seems that adding the next component to the global list when defining bundleMdx
was the trick such as below:
globals: {
"next/router": "Router",
"next/link": "Link",
"next/image": "Image",
},
And then when I call getMdxComponent
, we can pass in the imported next components with their corresponding key to the value described above:
import Link from "next/link";
import Image from "next/image";
import { Router } from "next/router";
...
const Component = useMemo(
() => getMDXComponent(code, { Link: Link, Image: Image, Router: Router }),
[code]
);
This allowed me to import a component in my MDX file that uses one of the above Next components
mdx-bundler
version: ^5.2.1node
version: v16.3.0npm
version: 7.20.1Relevant code or config
What you did: Thank you for an amazing project! I try to import
EsstentialismGraph
component to the MDX file but it turns out I get an error about the process. I figured out the problem was I usedImage
component from next.What happened:
Reproduction repository:
Problem description:
Suggested solution: