#using Exports key value in a tsc project

12 messages · Page 1 of 1 (latest)

golden mural
#

Hi, I am working on a npm package where I need to use exports to create an import from sub folder.
This is how my package.json looks and I removed main, types key from the json as I am using the exports.

For some reason my exports is not working and when I import any component from the package I see Cannot find module 'uikit' or its corresponding type declarations.

Do I need to make any adjustments in my ts config to make the exports work?

{
  "name": "uikit",
  "version": "0.4.4",
  "description": "",
  "homepage": "https://github.com/JohnVersus/nimbleuikit",
  "files": [
    "build"
  ],
  "exports": {
    ".": {
      "import": "./build/index.js",
      "require": "./build/index.js",
      "types": "./build/index.d.ts"
    },
    "./utils": {
      "import": "./build/utils/index.js",
      "require": "./build/utils/index.js",
      "types": "./build/utils/index.d.ts"
    }
  },
  ...
}

#

Since I removed main and types key I cannot import any component from the package, which shows that exports is not working

random light
#

Do you have your module/moduleResolution settings set correctly?

#

If memory serves, those keys require node16 or nodenext, but I’m not certain

golden mural
#

I currently have these in tsconfig

    "module": "esnext",
    "moduleResolution": "node",
random light
#

From what I’m seeing, it has to be node16 or nodenext (not sure if it’s one, the other, or both of those keys)

#

If you want to allow it to “fall back” to the old resolution style, you should keep “main”

atomic copper
#

"main" is also useful for being able to reference it in your scripts, i.e "start": "node ." will run the file specified in "main"

golden mural
#

With main I am able to import the root index. I am trying to import modules from sub folder as uikit/utils.
Will try using node16 or nodenext and let you know if it works.

marsh wadi
#

@golden mural im not 100% clear on subpath exports but you might need to define paths in your tsconfig

random light
#

Shouldnt matter in this case, we're talking about node_modules

#

I thought subpaths were imported relative to main, but maybe not