Skip to content

Add conditional exports to package.json #815

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

Merged
merged 2 commits into from
Mar 14, 2025

Conversation

lukescott
Copy link
Contributor

@lukescott lukescott commented Mar 6, 2025

I'm in the process of updating to Node 22 and I ran into an issue with the default export not working:

src/signature.tsx:25:18 - error TS2351: This expression is not constructable.
  Type 'typeof import("./node_modules/signature_pad/dist/types/signature_pad")' has no construct signatures.

25   this.pad = new SignaturePad(this.canvas, {

This PR fixes compatibility with TypeScript's "nodenext" module resolution. The "module" property is non-standard and not supported by node. The official way is "Conditional Exports":

https://nodejs.org/docs/latest-v22.x/api/packages.html#conditional-exports

This is needed for packages that support CommonJS and ESM where the "type" is omitted or set to "commonjs".

(Note: "require" could be omitted, but I was more explicit including both "require" and "default")

package.json Outdated
"exports": {
"import": "./dist/signature_pad.js",
"require": "./dist/signature_pad.umd.js",
"default": "./dist/signature_pad.umd.js",
Copy link
Collaborator

@UziTech UziTech Mar 6, 2025

Choose a reason for hiding this comment

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

Looks like it has a problem with the trailing comma in this line

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

Copy link
Collaborator

@UziTech UziTech left a comment

Choose a reason for hiding this comment

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

Thanks! 💯

@UziTech UziTech merged commit 005a090 into szimek:master Mar 14, 2025
5 checks passed
github-actions bot pushed a commit that referenced this pull request Mar 14, 2025
## [5.0.5](v5.0.4...v5.0.5) (2025-03-14)

### Bug Fixes

* Add conditional exports to package.json ([#815](#815)) ([005a090](005a090))
Copy link

🎉 This PR is included in version 5.0.5 🎉

The release is available on:

Your semantic-release bot 📦🚀

@niklaas
Copy link

niklaas commented May 29, 2025

For posterity, this could be considered a breaking change, see https://nodejs.org/docs/latest-v22.x/api/packages.html#package-entry-points:

Existing packages introducing the "exports" field will prevent consumers of the package from using any entry points that are not defined, including the package.json (e.g. require('your-package/package.json')). This will likely be a breaking change.

To make the introduction of "exports" non-breaking, ensure that every previously supported entry point is exported. (...)

Our builds started failing and it took me a bit to figure out what's going on. In parts of our codebase we depended on signature_pad like this

require('signature_pad/dist/signature_pad.umd')

or

import SignaturePad from 'signature_pad/dist/signature_pad.umd'

which threw errors such as

  ERROR in ./<redacted>.js 142:0-47
  Module not found: Error: Package path ./dist/signature_pad.umd is not exported from package /<redacted>/node_modules/signature_pad (see exports field in /<redacted>/node_
modules/signature_pad/package.json)

This can be fixed by changing the above to

require('signature_pad')

and

import SignaturePad from 'signature_pad'

respectively.

@UziTech
Copy link
Collaborator

UziTech commented May 29, 2025

17485225776322109827799301360532

@BoussonKarel
Copy link
Contributor

BoussonKarel commented May 29, 2025

I think @niklaas' point is not the fact that we should not have added this, but that the semver was not respected.

v5 should have the old paths listed undert he exports key for backwards compatibility as specified in the node docs.

  "exports": {
    "import": "./dist/signature_pad.js",
    "require": "./dist/signature_pad.umd.js",
    "default": "./dist/signature_pad.umd.js",
    "./dist/signature_pad.js": "./dist/signature_pad.js",
    "./dist/signature_pad.min.js": "./dist/signature_pad.min.js",
    "./dist/signature_pad.umd.js": "./dist/signature_pad.umd.js",
    "./dist/signature_pad.umd.min.js": "./dist/signature_pad.umd.min.js",

  },

And then in 6.0.0 remove it

@UziTech
Copy link
Collaborator

UziTech commented May 29, 2025

Yes and my point is that if we need to change the major version every time it could break someone using it the way it wasn't intended to be used we would only ever release major versions.

@niklaas
Copy link

niklaas commented May 29, 2025

I wrote this hoping to help someone else if they come across the same issue in the future.

@lukescott
Copy link
Contributor Author

lukescott commented May 29, 2025

The intention was to preserve backwards compatibility, and not knowing that import/require/defaults covered more than what "main" and (non-standard) "module" covered. If adding ./dist/ to the exports fixes the issue, a PR can certainly be opened and merged in to fix the breaking change. And I think that is worth doing, as more people may experience it over time.

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

Successfully merging this pull request may close these issues.

4 participants