Skip to content

Can't import components that use external packages #302

@olayway

Description

@olayway

What happens

When I try to import a custom React component that uses a 3rd party packages into my MDX files, I get this error:

 > ../components/LibTest.jsx:1:23: error: Could not resolve "date-fns" (mark it as external to exclude it from the bundle)
    1 │ import { format } from 'date-fns'
      ╵                        ~~~~~~~~~~

Error: Found 1 problems in 1 documents.

 └── Encountered unexpected errors while processing of 1 documents. This is possibly a bug in Contentlayer. Please open an issue.
     
     • "test/components-import.md": UnexpectedMDXError: Error: Build failed with 1 error:
     ../components/LibTest.jsx:1:23: error: Could not resolve "date-fns" (mark it as external to exclude it from the bundle)

The component I'm trying to import:

// LibTest.jsx
import { format } from 'date-fns'

export default function LibTest() {
  return <p>{format(new Date(2014, 1, 11), 'yyyy-MM-dd')}</p>
}

However, when I try to define this component directly in my MDX file and import the package it depends on, everything works just fine.

### Some MDX file
Lorem ipsum ...

import { format } from 'date-fns'

export function LibTest() {
  return <p>{format(new Date(2014, 1, 11), 'yyyy-MM-dd')}</p>
}

<LibTest/>

Steps to reproduce

  1. Clone this repo: https://github.com/flowershow/flowershow
  2. Go to templates/default and run npm install and npm run dev
  3. Open site/content/test/components-import.md
  4. Go to "Importing a custom component that uses a 3rd party package" section and uncomment these two lines:
{/*import LibTest from "../components/custom/LibTest.jsx"*/}
{/*<LibTest/>*/}
  1. Go to the next section: "Importing and using external package directly in MDX" and comment out/remove these lines:
import { format } from 'date-fns'

export function LibTest() {
  return <p>{format(new Date(2014, 1, 11), 'yyyy-MM-dd')}</p>
}

<LibTest/>
  1. 🐛 You should see the above error in the console.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions