MDX not exporting/importing js files as expected

Viewed 257

I am attempting to load a .js file into my markdown file in React using gatsby-plugin-mdx. However, I am running into an import error, which shows as a WARN as follows:

warn ./.cache/caches/gatsby-plugin-mdx/mdx-scopes-dir/7cd2ab5f9fe4b662b5bb93052ee9e f2c.js Attempted import error: 'Test2' is not exported from '../../../../src/pages/test2'
(imported as 'Test2').

Looking at the cache, it seems benign enough:

import { Test2 } from "../../../../src/pages/test2";
import * as React from 'react';
export default {
  Test2,
  React
};

The contents of the import seem arbitrary, and the warn will be the same (and contents will not load). I used a simple export default function returning a h1 tag to experiment, for example.

My .md file is fairly straight forward:

# Testing 1 
top
import { Test2 } from "../pages/test2"
<Test2 />
bottom

I suspected this is a configuration issue, but I'm yet to resolve it in my gatsby-config.js, which is currently as the following:

module.exports = {
  siteMetadata: {
    title: 'Marco Sousa Blog',
    author: 'Marco Sousa'
  },

  plugins: [
    'gatsby-plugin-dark-mode',
    'gatsby-plugin-react-helmet',
    'gatsby-plugin-sass',
    'gatsby-plugin-sharp',
    {
      resolve: 'gatsby-source-filesystem',
      options: {
        name:'src',
        path: `${__dirname}/src/`
      }
    },
    {
      resolve: 'gatsby-transformer-remark',
      options: {
        plugins: [
          'gatsby-remark-relative-images',
          {
            resolve: 'gatsby-remark-images',
            options: {
              maxWidth: 750,
              linkImagesToOriginal: false
            }
          },
          {
            resolve: `gatsby-remark-katex`,
            options: {
              // Add any KaTeX options from https://github.com/KaTeX/KaTeX/blob/master/docs/options.md here
              //strict: `ignore`,
             
            }
          }, 
          {
            resolve: `gatsby-remark-prismjs`,
            options: {
              classPrefix: "language-",
              inlineCodeMarker: null,
              aliases: {},
              showLineNumbers: true,
              noInlineHighlight: false,

              languageExtensions: [ 
                {
                  language: "superscript",
                  extend: "javascript",
                  definition: {
                    superscript_types: /(SuperType)/,
                  },
                  insertBefore: {
                    function: {
                      superscript_keywords: /(superif|superelse)/,
                    },
                  },
              },
            ],
            prompt: {
              user: "root",
              host: "localhost",
              global: false,
            },
            // By default the HTML entities <>&'" are escaped.
            // Add additional HTML escapes by providing a mapping
            // of HTML entities and their escape value IE: { '}': '&#123;' }
            escapeEntities: {},
            }
          },
        ]
      }
    },
    {
      resolve: "gatsby-plugin-page-creator",
      options: {
        path: `${__dirname}/src/`,
      },
    },
    {
      resolve: `gatsby-plugin-mdx`,
      options: {
        extensions: [`.mdx`, `.md`],
      },
    }, 
  ],
}

I think my querying/slug usage is fine, as the site and posts are operable prior (and after) to attempting to implement mdx. Observing the gatsby-plugin-mdx docs, I attempted to use gatsbyRemarkPlugins to encapsulate the other plugins, but this did not change the warn. What, more exactly, do you think could be the problem with my mdx usage?

1 Answers

This issue was in the way I imported. I simply changed

import { Test2 } from "../pages/test2"
<Test2 />

to

import Test2 from "../pages/test2"
<Test2 />

Test2.js was not a named export/import as I had imagined. Rather Test2.js used the default export, which does not need to be wrapped with curly braces.

Related