David Bradshaw
David Bradshaw

Reputation: 13077

import string into Astro MDX

I need to import a simple string into an astro starlight MDX file to show the latest version number. My code is basically

---
title Version Test
desc Show version
---
import VERSION from "/src/components/version";

Version {VERSION}

and version.js is just

export const VERSION = '1.0.0'

I'm able to successfully import this into a .astro file, but in my MDX file it is loading as undefined and not logging any error messages.

How can I import this simple string into my MDX file?

Upvotes: 0

Views: 154

Answers (2)

marcelofreires
marcelofreires

Reputation: 816

The problem is the import type. You exported a named const and need import as named on the MDX file, like Dogbert suggested in a comment:

Try import {VERSION} from "/src/components/version"; braces around VERSION.

I forked an example for MDX from the Astro's github examples and modified for your case on the StackBlitz.

Upvotes: 1

Dogbert
Dogbert

Reputation: 222198

To import a named export in ES6 modules, you need to use destructuring in the import call:

import { VERSION } from "/src/components/version";

Version {VERSION}

Upvotes: 0

Related Questions