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.

I cannot import the component into my mdx file

See original GitHub issue

When i was trying to import the components into my mdx file i got this error

Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

I am using nextjs 11 with latest mdx-bundler version here is my file which bundler mdx to html

import fs from 'fs'
import path from 'path'
import matter from 'gray-matter'

import { bundleMDX } from 'mdx-bundler'
if (process.platform === 'win32') {
  process.env.ESBUILD_BINARY_PATH = path.join(
    process.cwd(),
    'node_modules',
    'esbuild',
    'esbuild.exe',
  );
} else {
  process.env.ESBUILD_BINARY_PATH = path.join(
    process.cwd(),
    'node_modules',
    'esbuild',
    'bin',
    'esbuild',
  );
}
export const contentPath = path.join(process.cwd(), 'articles')
export const componentPath = path.join(process.cwd(), 'src/components');
export const getMdxSource = (mdxFile) => {
  return fs.readFileSync(path.join(contentPath, mdxFile));
}
export const getSortedContent = () => {
  return fs.readdirSync(contentPath).filter((path) => /\.mdx?$/.test(path)).map((mdxFile) => {
    const source = getMdxSource(mdxFile);
      const slug = mdxFile.replace(/\.mdx?$/, "");
      const { data } = matter(source);

      return {
        frontmatter: data,
        slug: slug,
      }
  }).sort((a, b) => {
    return new Date(a.frontmatter?.date) < new Date(b.frontmatter?.date) ? 1 : -1;
  });
}
export const getContentbyData = async(slug) => {
  const source = getMdxSource(slug + ".mdx");
  const { code, frontmatter } = await bundleMDX(source, {
    cwd: process.cwd(),
 })

  return {
    frontmatter,
    code,
  };
}

my mdx file

---
title: What is Programming ?
author: IshanKBG
date: 07-24-2021
---
import Hello from "src/components/Hello"

Before knowing what is programming you must know the what are computers and how they work.
I know that most of you know what are computers and you are actually using it to read this article.
If I say in simple words computers are just machines performing arithmetical and logical operations for you. Computers run programs which contains some sets of instructions to perform different tasks for you and programming is a process of writing set of instructions to tell computer to perform certain task. For example imagine you are trying to make a cake and to make it you have to follow some sort of instructions.
Those instructions must be in an arranged and sequenced manner else your cake will not become as you want to be as you can’t add eggs after baking. Making a cake is a same as writing programs as you have to write each and every piece of code in an arranged and sequenced manner in order to make it work as intended.
# Should I learn programming ?
<Hello />
This type of question has multiple answers, it depends on the person who is willing to either learn it or not. For some people programming is just a job, for some it’s only business or money and for some people it’s a grand passion of their life. Some want to learn programming so that they can stimulate robotic hands, for some it’s game development and for some it’s web development. It depends on where and how you want to apply programming skills. So ask this question to you and you will find the answer
Some might ask that why did I learn programming and my answer is that I have always been intrigued by computers and thought of them as a means to bridge the gap between countries and entire continents, allowing people from various communities to communicate with each other, I was fascinated by how it all worked and so I decided to pursue Programming as my career choice.

Github repo: https://github.com/IshanKBG/ishankbg.dev

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
IshanKBGcommented, Jul 27, 2021

Actually this issue got fixed after i changed the extension from .js to .jsx. I am not sure why this is issue getting caused

0reactions
IshanKBGcommented, Jul 30, 2021

Ya it’s now working

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to handle an imported components imports #53 - GitHub
The imported component in the MDX file is fine but if that import has it's own imports it throws an error. Is there...
Read more >
Troubleshooting MDX
This article goes through several common problems and errors that might occur when using MDX.
Read more >
import components into a mdx file that is outside the src folder ...
I try to import some components in /content/pages/about/index.mdx. I tried to import the footer (to testing) with this code which returns: ...
Read more >
MDX - Storybook
Storybook is a frontend workshop for building UI components and pages in isolation. ... MDX is a standard file format that combines Markdown...
Read more >
Troubleshooting - Astro Documentation
Astro components run on the server, so you can't access these ... to see that you have imported the component in your .astro...
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