Skip to content

ESM packages doesn't work #1046

@boyeln

Description

@boyeln

Affected Packages

pkg:read

(probably more, if not all, but I've only looked into read)

Problem

Reproduction: https://github.com/boyeborg/changesets-esm-issue-repro

When importing @changesets/read in an ESM module, and using the default exported function:

import readChangesets from "@changesets/read";

const cwd = process.cwd();

readChangesets(cwd).then(console.log);

You'll get the following error:

readChangesets(cwd).then(console.log);
^

TypeError: readChangesets is not a function

It seems like it only uses the CJS version. You can work around it by accessing the function through readChangesets.default.

Proposed solution

I'm no expert at this, but to me it looks like a misconfiguration. However, there are several issues.

First off, I don't think it's sufficient just to set "module": "<path>" in package.json. I think you also need to add some exports, like:

"exports": {
  ".": {
    "require": "./dist/read.cjs.js",
    "import": "./dist/read.esm.js"
  }
}

Secondly, I don't think you can use the .esm.js extension. I believe you have to use .mjs since you don't have "type": "module"in your package.json-file.

Lastly, there are some issues with the ESM file (dist/read.esm.js) itself where it's doing some named CJS imports, which are not allowed in ESM. Hence, it's not a valid ESM file.

I guess all of these issues might originate from Preconstruct, which I'm not familiar with, so I cannot tell if it's some configuration change that can be made, or if it's a downstream issue.

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