I want to take advantage of the new-ish "exports" feature of Node.js/package.json so that I can do the following:
"exports": {
".": "./dist/index.js",
"./foo": "./dist/path/to/foo.js"
}
and users can do the following:
import { foo } from 'my-package/foo';
TypeScript 4.5 should support the "exports" field, yet it does not seem to work. I am building a simple package using TS 4.5.2, and I am consuming that package in a project using TS 4.5.2. I have looked at other SO questions and this GitHub thread and this bug report but can't seem to find a consensus on the issue and whether it should work today.
I am still able to import using the more verbose syntax:
import { foo } from 'my-package/dist/path/to/foo.js';
I have also tried the object notation for exports, to no avail:
"exports": {
".": { "require": "./dist/index.js", "import": "./dist/index.js" },
"./foo": { "require": "./dist/path/to/foo.js", "import": "./dist/path/to/foo.js" }
}
Is this feature ready to be used with TypeScript projects today? If yes, what am I missing? Specifics about tsconfig would be useful for both the source project and consuming project. The TS compiler complains about node12/nodenext being used for either the module or moduleResolution fields (I am definitely using TS 4.5.2).
moduleResolution, notmodule?exportsand it should work as expected. I recommend starting small and going from there. If you're trying to retrofit an existing project, something else is probably getting in your way. This whole process if fairly simple and straightroward. The most complex part is the tsconfig. See thetl;drsection above - that should be all you need, but I'd recommend following a recent blog post for the most up-to-date instructions.