Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 649
Bram Vanbilsen Avatar asked Dec 21 '25 21:12

Bram Vanbilsen


1 Answers

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.

like image 174
Bram Vanbilsen Avatar answered Dec 24 '25 09:12

Bram Vanbilsen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!