Skip to content

Point main at the CJS build so named imports resolve under Node - #56

Open
nandastone wants to merge 1 commit into
kunukn:masterfrom
nandastone:fix/main-points-at-umd
Open

Point main at the CJS build so named imports resolve under Node#56
nandastone wants to merge 1 commit into
kunukn:masterfrom
nandastone:fix/main-points-at-umd

Conversation

@nandastone

@nandastone nandastone commented Aug 24, 2026

Copy link
Copy Markdown

Importing the library by name gives you undefined wherever Node's resolver does the work:

import { Collapse } from '@kunukn/react-collapse'
// undefined under vitest, jest, or Next SSR

React then reports Element type is invalid ... got: undefined, which names neither the import nor this package.

Bundlers are fine: they read module and get the ESM build. Node reads main, which points at the UMD bundle, and that bundle ends by assigning onto the factory's namespace argument, n.Collapse = P. cjs-module-lexer only sees statically analysable assignments, so Node concludes the module has no named exports. dist/react-collapse.cjs already ships beside it and ends exports.Collapse = B, which the lexer does see. main has never pointed there.

-  "main": "dist/react-collapse.umd.js",
+  "main": "dist/react-collapse.cjs",
+  "exports": {
+    ".": {
+      "types": "./dist/react-collapse.d.ts",
+      "import": "./dist/react-collapse.mjs",
+      "require": "./dist/react-collapse.cjs"
+    },
+    "./dist/*": "./dist/*",
+    "./package.json": "./package.json"
+  },

The exports map is the part worth your attention, since you removed one in 74a7131 and I'd rather not undo that. Reading the commit, the map looks like it was in the way: the same change added require('@kunukn/react-collapse/dist/react-collapse.cjs') to the CommonJS playground, and a map blocks any subpath it does not list, so that require would have thrown ERR_PACKAGE_PATH_NOT_EXPORTED. Dropping the map is what opened the deep-link route you gave the reporter in #51.

./dist/* keeps that route open by passing every path through. I checked the playground's deep require, the CSS import and the documented types path against an installed 3.0.15 with this patched in, along with the bare named import and require('@kunukn/react-collapse').

The UMD build is untouched and still shipped, so the CDN usage in _playground_global-js is unaffected.

The UMD bundle assigns its export inside the factory, where Node's CJS
lexer cannot see it, so `import { Collapse }` resolves to undefined for
anything using Node resolution. dist/react-collapse.cjs already ships and
ends with a plain `exports.Collapse`, which is detectable.

Restores the exports map removed in 74a7131, against the current
filenames, with a ./dist/* passthrough so the deep paths the README
documents keep working.
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

Successfully merging this pull request may close these issues.

1 participant