Add exports field to package.json in order to support moduleResolution node12/nodenext
See original GitHub issue🚀 Feature request
Current Behavior
When using "moduleResolution": "NodeNext",
in your tsconfig it expects an "exports": ...
field in package.json in order to resolve the package.
Otherwise:
Cannot find module 'fp-ts/Either' or its corresponding type declarations.ts(2307)
Desired Behavior
Importing to work.
Suggested Solution
Shove this in package.json:
"exports": {
".": {
"import": {
"types": "./es6/index.d.ts",
"default": "./es6/index.js"
},
"require": {
"types": "./lib/index.d.ts",
"default": "./lib/index.js"
}
},
"./*": {
"import": {
"types": "./es6/*.d.ts",
"default": "./es6/*.js"
},
"require": {
"types": "./lib/*.d.ts",
"default": "./lib/*.js"
}
},
"./es6/*": {
"import": {
"types": "./es6/*.d.ts",
"default": "./es6/*.js"
}
},
"./lib/*": {
"require": {
"types": "./lib/*.d.ts",
"default": "./lib/*.js"
}
}
},
Tested it locally by putting that above "main"
like in the example as seen in the TS 4.7 link below, seems to do the job.
The "./es6/*"
or "./lib/*"
parts are only needed for backwards-compatibility with the old import paths.
Who does this impact? Who is this for?
People wanting to use fp-ts
with the new ECMAScript Module Support added with TypeScript 4.7.
Describe alternatives you’ve considered
Not using the new ECMAScript Module support feature. 🤷♂️ Most NPM packages aren’t updated to support this yet.
Additional context
TS 4.7 link: https://devblogs.microsoft.com/typescript/announcing-typescript-4-7/#esm-nodejs Node.js link: https://nodejs.org/api/packages.html#package-entry-points
Your environment
Software | Version(s) |
---|---|
fp-ts | 2.12.1 |
TypeScript | 4.7.3 |
Issue Analytics
- State:
- Created a year ago
- Reactions:5
- Comments:11 (5 by maintainers)
The above suggestion is mostly correct, but there’s a caveat with the file extensions. Depending on the the value of the
type
field inpackage.json
, Node will treat.js
files either as CJS or ESM, while it will always treat.cjs
file as the former and.mjs
files as the latter.To make Node happy we have to either:
./es6/index.js
to./es6/index.mjs
and./es6/*.js
to./es6/*.mjs
type: module
and rename./lib/index.js
to./lib/index.cjs
and./lib/*.js
to./lib/*.cjs
package.json
inside./es6
withtype: module
(and for good measure do the equivalent in./lib
)There’s also the option of dropping CJS completely, adding
type: module
inpackage.json
, and living a happier life.done