How to publish TypeScript modules on NPM without “dist” in import?

前端 未结 4 1609
暗喜
暗喜 2021-02-19 02:59

I\'m trying to publish a typescript library on NPM but I can\'t to have a \"good\" path after publishing.

I follow several guide on this point but I\'m not found a solut

4条回答
  •  萌比男神i
    2021-02-19 03:51

    I found a pretty clean solution that doesn't require copying your package.json into your dist or publishing from dist.

    You can structure your project as:

    package.json (for entire project)
    src
     | secondary-package-name
       |- index.ts
    secondary-package-name
     |- package.json (for secondary-module-name)
    dist
     |- generated files
    
    

    the package.json for your secondary module just needs to contain

    {
      "name": "package/secondary-package-name",
      "private": true,
      "main": "../dist/secondary-package-name/index.js"
    }
    
    

    to point back to your dist

    You should then be able to reference exports from the secondary package like so:

    import { someFn } from "package/secondary-package-name"
    

    provided it is exported from the index.ts file in that directory

    You'll also need to make sure the files field of your main package.json includes:

    "files": [
        "dist",
        "secondary-package-name"
      ],
    

提交回复
热议问题