Bram  Vanbilsen
Bram Vanbilsen

Reputation: 6505

Using package.json exports in a Typescript package

I have a local Typescript package that I am importing into a project using npm I ./path/to/midule. The package.json for this package looks as follows:

{
    "name": "my_package",
    "version": "1.0.0",
    "main": "./build/src/index.js",
    ...
}

I am trying to switch from using the main field to the newer exports field as I would like more control over how my package is imported. I tried to replace the main field with something I thought to be equivalent:

{
    "name": "my_package",
    "version": "1.0.0",
    "exports": {
        ".": "./build/src/index.js"
    },
    ...
}

This however, does not work. When trying to import the package into my other project, I'm getting the following error:

Cannot find module 'my_package' or its corresponding type declarations.

I am generating the .d.ts files using the declarations flag with the typescript compiler and specified provided the exact path to the index.d.ts file in package.json, but the same error message persisted.

I am probably missing something simple, but can't seem to find what it is. How can I use the exports field in a Typescript package?

Upvotes: 3

Views: 2128

Answers (1)

Bram  Vanbilsen
Bram Vanbilsen

Reputation: 6505

I ended up finding the answer:
You need to set the moduleResolution field in your tsconfig.json file to nodenext. I also needed to update to the latest LTS version of node.js.

Upvotes: 2

Related Questions