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

Support for custom conditional exports in npm specifier #23757

Open
nestarz opened this issue May 9, 2024 · 0 comments
Open

Support for custom conditional exports in npm specifier #23757

nestarz opened this issue May 9, 2024 · 0 comments
Labels
node compat suggestion suggestions for new features (yet to be agreed)

Comments

@nestarz
Copy link

nestarz commented May 9, 2024

What is the problem this feature would solve?

Deno currently does not provide a way to specify custom export conditions when importing npm packages. This makes it impossible to import non-default exports that are conditionally exported based on certain conditions, such as the "react-server" condition used by React Server Components.

See:
https://nodejs.org/api/packages.html#conditional-exports
https://github.com/reactjs/rfcs/blob/main/text/0227-server-module-conventions.md

What is the feature you are proposing to solve the problem?

Add support for a config flag in Deno, similar to the one supported by Node.js and Bun. This flag would allow specifying custom export conditions when importing npm packages.

For example, in Bun, thanks to this issue oven-sh/bun#8990 you can do:

bun --conditions special-condition

Then, if you import an npm package, it will follow the conditions specified in the package's exports field.

Ideally, it would be great to set up this condition inside the deno.json config, so that it can be used by Deno Deploy as well. An other option would be to specify it directly in the npm specifier, so that no config file are needed ?

What alternatives have you considered?

One workaround would be to directly import the specific file path, like:

import "npm:/react@19.0.0-beta-b498834eab-20240506/cjs/react.react-server.development.js"

However, this doesn't work if the file is not explicitly exported in the package's exports field:

Uncaught TypeError: Failed resolving package subpath './cjs/react.react-server.development.js' for '/Users/Library/Caches/deno/npm/registry.npmjs.org/react/19.0.0-beta-b498834eab-20240506/package.json'
  Caused by:
    [ERR_PACKAGE_PATH_NOT_EXPORTED] Package subpath './cjs/react.react-server.development.js' is not defined by "exports" in '/Users/Library/Caches/deno/npm/registry.npmjs.org/react/19.0.0-beta-b498834eab-20240506/package.json' imported from '/Users/code/$deno$repl.ts'

I think this is intentional from the Deno team to not expose what's not exposed through the exports option. But then, Deno should provide a way to define the export conditions like Bun and Node does.

@dsherret dsherret added suggestion suggestions for new features (yet to be agreed) node compat labels May 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
node compat suggestion suggestions for new features (yet to be agreed)
Projects
None yet
Development

No branches or pull requests

2 participants