Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot import "browser" version from ESM package #283

Open
nfarina opened this issue Apr 19, 2024 · 0 comments
Open

Cannot import "browser" version from ESM package #283

nfarina opened this issue Apr 19, 2024 · 0 comments

Comments

@nfarina
Copy link

nfarina commented Apr 19, 2024

Describe the bug

My package is 100% ESM, and I'm using Vite for development and production builds.

To Reproduce
Create any blank Vite project and add+import Eta

Expected behavior
browser.module.mjs should be used instead of eta.module.mjs

Screenshots

vite v5.2.8 building for production...
[plugin:vite:resolve] [plugin vite:resolve] Module "node:path" has been externalized for browser compatibility, imported by "/Users/nick/Code/burnside-web/node_modules/eta/dist/eta.module.mjs". See https://vitejs.dev/guide/troubleshooting.html#module-externalized-for-browser-compatibility for more details.

Package & Environment Details

  • Environment: Vite
  • Version: 5.2.8

Additional context
This was tackled in #146, and the suggested workaround was detailed in #242, namely to use:

import { Eta } from "eta/dist/browser.module.mjs";

However, this does not work in an ESM project that supports the exports keyword in package.json - since this import isn't explicitly defined there!

This modified exports key in package.json fixes the issue for me:

  "exports": {
    ".": {
      "types": "./dist/types/index.d.ts",
      "import": "./dist/eta.module.mjs",
      "browser": "./dist/browser.umd.js",
      "require": "./dist/eta.umd.js",
      "default": "./dist/eta.umd.js"
    },
    "./browser": {
      "types": "./dist/types/index.d.ts",
      "default": "./dist/browser.module.mjs"
    }
  },

With this, I can write:

import { Eta } from "eta/browser";

And everything works as expected, including types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant