Description
package.json exports
are only supported in --moduleResolution node16
and --moduleResolution nodenext
. This is expected behavior, but we are working on expanding it. A few points of common confusion:
- If you’re using Node 12 or 14, you should use
--moduleResolution node16
. The features added to 16 have fully or mostly been backported to 12 and 14. --moduleResolution node
is for Node 11 and older. Its name is currently bad. We plan to rename it accordingly.- If you are using a bundler or a non-Node runtime, we do not currently have a mode for you.
node16
andnodenext
are only for Node.*
This has been thoroughly discussed elsewhere, so I won’t go into much more detail—I’m creating this just to have an issue to track and point to, since #33079 is closed, but people continue to show up thinking it’s incomplete due to lack of support outside of Node. If you want to participate in discussion about current or future support for package.json exports
or module resolution outside of Node more generally, it is critical to read #50152 first. Please do not comment here without reading that context.
* It’s worth noting that the (intentional) lack of support for package.json exports
in the node
(to be renamed node11
soon) mode can be, and has been, worked around for bundler users by many libraries in one of two ways:
(EDIT: This is now documented in detail at https://github.com/andrewbranch/example-subpath-exports-ts-compat)
- Arranging index files and subdirectory package.json files of the library such that Node 11 (non-exports-respecting) and Node 12+ (exports-respecting) reach the same result
- Setting a
typesVersions
that mirrors the structure ofexports
such that TypeScript’s--moduleResolution node
will reach the same result as TypeScript’s--moduleResolution nodenext
. Note that in this scenario, other resolvers that don’t respectexports
, like Node 11, Parcel, and Browserify, will be broken. The syntax fortypesVersions
is very similar, but not identical, to that ofexports
. If you are a library author who intends to support Webpack, esbuild, Vite, Node 12+, etc., but not Parcel, Browserify, or Node 11, and you need help figuring out how to set uptypesVersions
as an interim workaround, feel free to ask here and I’ll assist.