Skip to content

fix: add package.json exports map so Node, Vitest and SSR can import the package - #395

Open
pcbeingused333 wants to merge 3 commits into
yWorks:masterfrom
pcbeingused333:fix/package-exports-394
Open

pcbeingused333 wants to merge 3 commits into
yWorks:masterfrom
pcbeingused333:fix/package-exports-394

Conversation

@pcbeingused333

Copy link
Copy Markdown

Fixes #394

Since 2.8.0 package.json sets "type": "module" while "main" still points to the UMD bundle dist/svg2pdf.umd.min.js and there is no exports map. Node, Vitest and SSR resolvers therefore evaluate the UMD file as an ES module, the UMD wrapper falls through to its browser-global branch and throws TypeError: Cannot read properties of undefined (reading 'jsPDF') at import time. The named import { svg2pdf } also fails because the UMD bundle has no ESM export.

Add an exports map: import resolves to the existing ES build, require to a new CommonJS bundle dist/svg2pdf.umd.min.cjs (a .cjs extension is required because "type": "module" makes .js files ESM). main now points to the .cjs file, and ./dist/* stays exported so the existing UMD/script-tag paths (README, playground) keep working. The new bundle is produced by an extra output in rollup.config.mjs; the other dist files are byte-identical after rebuilding.

Reproduction

In a project with svg2pdf.js and jspdf installed (as in the issue), node repro.mjs with:

import { jsPDF } from 'jspdf'
import 'svg2pdf.js'
console.log(typeof jsPDF.API.svg)

On current master this throws TypeError: Cannot read properties of undefined (reading 'jsPDF') from dist/svg2pdf.umd.min.js; import { svg2pdf } from 'svg2pdf.js' fails with SyntaxError: ... does not provide an export named 'svg2pdf'. With this change both work, and require('svg2pdf.js') loads the CJS build.

Testing

test/node-entry/node-entry.test.mjs (npm run test:node, plain node:test, after npm run build) builds a throwaway consumer project (temp dir with node_modules/svg2pdf.js -> this checkout and jspdf) and runs node on the three cases above (side-effect import, named import, require). 0/3 on master, 3/3 with the change. oxfmt --check and oxlint are clean on the touched files. The browser vitest suite was not run (needs Playwright); it does not go through package resolution.

dist/ is not part of this diff (it only changes in release commits via the version script, which will add dist/svg2pdf.umd.min.cjs and its map); npm run build generates the new files locally.

AI assistance: this change and its regression test were prepared using Claude Code.

…the package

With "type": "module" and "main" pointing at a .js UMD bundle, resolvers
evaluate the UMD file as ESM and it throws at import time. Add an exports map
(import -> ES build, require -> new .cjs bundle) and a node:test regression test.

Fixes yWorks#394
@yGuy

yGuy commented Nov 17, 2026

Copy link
Copy Markdown
Member

awesome!
Thoughts: This should probably not be in a bugfix release as it changes important meta-data. Even though this was buggy before, there were cases (or workarounds in place) where this worked and this could now break.

Also: can we also test proper typescript tsc --noemit?

Cover Node16, NodeNext and Bundler resolution for the package exports.

Refs yWorks#395.
@pcbeingused333

Copy link
Copy Markdown
Author

Agreed that this metadata change should wait for a release beyond a bugfix release. Added tsc --noEmit consumer checks for Node16, NodeNext and Bundler resolution, including the named export, options and jsPDF augmentation. All six Node/TypeScript consumer tests pass, as does the repository typecheck. The consumer type checks cover ESM imports; the existing CommonJS check covers runtime loading.

No version bump is included; release scheduling remains with the maintainers.

This comment was drafted with Claude Code.

@yGuy
yGuy requested a review from HackbrettXXX November 17, 2026 10:23

@HackbrettXXX HackbrettXXX left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Can you add the new test:node script to the continuous-integration workflow?

Builds dist/ first because the Node consumer test imports the built package.

Signed-off-by: Alex Castillo <alex.castillog33@gmail.com>
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.

2.8.0+ throws on import/require under Node, Vitest, and SSR because "type": "module" runs UMD main as ESM

3 participants