Skip to content

Broken ESM support #252

@sai-cb

Description

@sai-cb

When this library is imported under node as ESM, named exports are broken.

Reproduction steps

This can be reproduced by creating these files:

demo.mjs:

import { Helmet } from 'react-helmet-async';

console.log(Helmet);

demo.cjs:

const { Helmet } = require('react-helmet-async');

console.log(Helmet);

And then running them:

  • node demo.mjs
  • node demo.cjs

Running the .mjs demo file leads to the following crash currently:

import { Helmet } from 'react-helmet-async';
         ^^^^^^
SyntaxError: Named export 'Helmet' not found. The requested module 'react-helmet-async' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'react-helmet-async';
const { Helmet } = pkg;

Solution

This seems to be fixable by adding the currently missing exports key in package.json:

  "exports": {
    ".": {
      "types": "./lib/index.d.ts",
      "import": "./lib/index.esm.js",
      "require": "./lib/index.js"
    }
  },

This preserves CJS compatibility but fixes ESM support.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions