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

前端 未结 4 1603
暗喜
暗喜 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条回答
  •  抹茶落季
    2021-02-19 03:33

    This might be a little different than what you were looking for, but I think this is the more standard approach. Instead of trying to import from different directories in your module, just use a single main.ts file to gather everything up for export. Assuming that you have this main.ts file in your lib directory that re-exports everything, you can just configure the package.json to point to the generated JS.

    Folder structure:

    dist
     |- main.js
     |- main.d.ts
     |- all other generates files
    lib
     |- main.ts
    ...
    

    package.json:

    {
        ...
        "main": "./dist/main.js",
        "types": "./dist/main.d.ts",
        ...
    }
    

    In this example, if you have the following in your main.ts:

    export const testValue = 5;
    

    You could import it in other code that depends on this library by using import { testValue } from '';

    Check out the typescript docs for more info about this.

提交回复
热议问题