Skip to content

Incorrect "exports" in package.json #38

@kettanaito

Description

@kettanaito

First of all, thank you for a great package!

What

This library currently points to index.web.js for the import condition. That is incorrect. What ends up happening is that Node.js ESM (which resolves the import condition) tries to load the browser build of this package, resulting in a failure.

This package, basically, assumes that import is used only in the browser. Luckily, we can use ESM in Node.js now. I want to be able to use this package there too!

Proposal

Instead, provide environment-based export conditions to correctly distribute this package:

  "exports": {
    ".": {
      "types": "./index.d.ts",
-     "import": "./index.web.js",
+     "node": {
+       "import": "./index.node.mjs",
+       "require": "./index.node.js"
+     },
      "browser": "./index.browser.js",
      "require": "./index.node.js",
      "default": "./index.web.js"
    }
  },

Note that you can nest import/require/default/etc. export conditions inside an environment condition. We've been using that to a great effect in MSW for years now (ref).

Bundling

The node.import condition must points to an ESM build of this package for Node.js. If the said build target doesn't exist, let's add it.

Additional context

I am willing to open a pull request for this.

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