question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created a year ago
  • Reactions:5
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
andreavaccaricommented, Jun 29, 2022

The above suggestion is mostly correct, but there’s a caveat with the file extensions. Depending on the the value of the type field in package.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:

  1. Rename ./es6/index.js to ./es6/index.mjs and ./es6/*.js to ./es6/*.mjs
  2. Add type: module and rename ./lib/index.js to ./lib/index.cjs and ./lib/*.js to ./lib/*.cjs
  3. Add a package.json inside ./es6 with type: module (and for good measure do the equivalent in ./lib)

There’s also the option of dropping CJS completely, adding type: module in package.json, and living a happier life.

1reaction
gcanticommented, Sep 17, 2022

going to release 2.13.0-rc.3 with the fix asap

done

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add exports field to subpackages to align module resolution ...
This was the only way to allow multiple entries into a package so that you could do preact/hooks instead of preact/hooks/dist/hooks.module.js .
Read more >
Node.JS (New) Package.json Exports Field | by Thomas Juster
The documentation for the exports package.json field is here, it comes from this proposal. I won't cover everything, so check out the ...
Read more >
Package exports - webpack
The exports field in the package.json of a package allows to declare which module should be used when using module requests like import...
Read more >
How can I use "exports" in package.json today for nested ...
The exports field in package.json is supported by ANY npm package ... should be using moduleResolution : node , node16 , or nodenext...
Read more >
Modules: Packages | Node.js v19.3.0 Documentation
Existing packages introducing the "exports" field will prevent consumers of the package from using any entry points that are not defined, including the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found