How can I use “exports” in package.json for nested submodules and TypeScript?

  • Node.js has supported exports since v12.7.0 (Jul. 2019)

    When I asked this question (Dec. 2021), NodeJS had supported the exports field for nearly 2.5 years. It seemed reasonable to assume that Typescript supported it.

  • The latest version of TypeScript at that time (4.5) did not support the exports field.

    This was particularly confusing because the TS 4.5 beta announcement said that it would supportpackage.json exports.

  • Typescript 4.7 (Jun. 2022) finally supported package.json exports

  • Using typesVersions in package.json is not the solution

    Several people suggested using typesVersions – but that’s a completely separate feature which is specific to TypeScript only (read more about it here). The exports field in package.json is a feature of node and should work with any npm module.

So, if you have a Typescript project and you want to be able to import a package which uses package.json “exports”, you will need to do the following:

  • Your TypeScript project must be using TS v4.7 or later
  • Your tsconfig should be using moduleResolution of node16 or nodenext.
    • You don’t have to set moduleResultion if you are using module with a value of CommonJS, ES2015, ES6, ES2020, or ESNEXT

Leave a Comment

404 Not Found

Not Found

The requested URL was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.