OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

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

  • Thread starter Thread starter Ryan Wheale
  • Start date Start date
R

Ryan Wheale

Guest
I want to take advantage of the new-ish "exports" feature of Node.js/package.json so that I can do the following:

Code:
"exports": {
  ".": "./dist/index.js",
  "./foo": "./dist/path/to/foo.js"
}

and users can do the following:

Code:
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:

Code:
import { foo } from 'my-package/dist/path/to/foo.js';

I have also tried the object notation for exports, to no avail:

Code:
"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).

<p>I want to take advantage of the new-ish <a href="https://nodejs.org/api/packages.html#packages_exports" rel="noreferrer">"exports"</a> feature of Node.js/<code>package.json</code> so that I can do the following:</p>
<pre><code>"exports": {
".": "./dist/index.js",
"./foo": "./dist/path/to/foo.js"
}
</code></pre>
<p>and users can do the following:</p>
<pre><code>import { foo } from 'my-package/foo';
</code></pre>
<p>TypeScript 4.5 should <a href="https://devblogs.microsoft.com/type...kagejson-exports-imports-and-self-referencing" rel="noreferrer">support the "exports"</a> 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 <a href="https://stackoverflow.com/questions...son-exports-field-not-working-with-typescript">SO</a> <a href="https://stackoverflow.com/questions/53680866/how-to-export-a-submodule-in-a-typescript-library">questions</a> and <a href="https://github.com/microsoft/TypeScript/issues/33079" rel="noreferrer">this GitHub thread</a> and <a href="https://github.com/microsoft/TypeScript/issues/46339" rel="noreferrer">this bug report</a> but can't seem to find a consensus on the issue and whether it should work today.</p>
<p>I am still able to import using the more verbose syntax:</p>
<pre><code>import { foo } from 'my-package/dist/path/to/foo.js';
</code></pre>
<p>I have also tried the object notation for exports, to no avail:</p>
<pre><code>"exports": {
".": { "require": "./dist/index.js", "import": "./dist/index.js" },
"./foo": { "require": "./dist/path/to/foo.js", "import": "./dist/path/to/foo.js" }
}
</code></pre>
<p>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 <code>node12</code>/<code>nodenext</code> being used for either the <code>module</code> or <code>moduleResolution</code> fields (I am definitely using TS 4.5.2).</p>
 

Latest posts

Top