ESM/Vite SSR compatibility issue - "is not a function" error with import * as matter #181
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Description
Problem
When using
gray-matterin an ESM project with Vite SSR (e.g., Astro), the module cannot be properly imported and called.Environment
"type": "module"in package.json)esModuleInterop: true,allowSyntheticDefaultImports: trueError
Reproduction Steps
Create an ESM project with Vite or Astro:
Install gray-matter:
Import and use in a TypeScript file:
Run in Vite SSR mode:
Expected Behavior
The import should work and
matter()should be callable as a function.Actual Behavior
import matter from 'gray-matter'fails with TypeScript error: "module has no default export"import * as matter from 'gray-matter'imports a namespace object wherematter()is not callable at runtimeRoot Cause Analysis
The package only exports via CommonJS (
module.exports = matter) with:Missing:
exportsfield for proper ESM supportmodulefield for bundlers.mjsentry pointThe type definition uses
export = matterwhich is the CommonJS-style export:Suggested Fix
Add ESM support via the
exportsfield in package.json:And create an ESM wrapper (
index.mjs):Update type definitions to support both:
Current Workaround