-1

What is the correct way to export from a package.json file, such that:

some-file.ts

import { someAmazingFunction } from '@my-org/package/amazing"

i.e., we can import from a specific "sub-module" (I'll call it a sub-module, but please correct me if that isn't the correct terminology).

I thought the correct way to set this up would be the following in my package.json:

{
  "name": "@my-org/package",
  ...
  "type": "module",
  "files": [
    "dist"
  ],
  "main": "./dist/index.cjs",
  "module": "./dist/index.js",
  "types": "./dist/index.d.ts",
  "exports": {
    "amazing": {
      "import": "./dist/amazing.js",
      "require": "./dist/amazing.cjs",
      "types": "./dist/amazing.d.ts"
    }
  }
}

I'm using a slightly standard lib build in my vite.config.ts, with corresponding rollup options, and on build, I have the relevant amazing.* files in the dist folder.

However, when trying to import as:

import { } from '@my-org/package/amazing"

I get this error:

Cannot find module '@my-org/package/amazing' or its corresponding type declarations ...

One thing I have noticed, for the project I am importing into, this error goes away if I specify the "bundler" resolutionMode in the associated tsconfig.json.

Micheal J. Roberts
  • 3,735
  • 4
  • 37
  • 76

1 Answers1

0

use relative path like this:

{
  "name": "@my-org/package",
  ...
  "type": "module",
  "files": [
    "dist"
  ],
  "main": "./dist/index.cjs",
  "module": "./dist/index.js",
  "types": "./dist/index.d.ts",
  "exports": {
    "./amazing": {
      "import": "./dist/amazing.js",
      "require": "./dist/amazing.cjs",
      "types": "./dist/amazing.d.ts"
    }
  }
}
Tachibana Shin
  • 2,605
  • 1
  • 5
  • 9