publishing a typescript library on npm: exported symbols, modules

1.7k views Asked by At

I'm writing a typescript library which I intend to publish on npm. It is meant to run in node only, never in the browser. It will make sense to use the library only from typescript (I'm not expecting any Javascript users).

I have trouble finding guides how to publish typescript-for-typescript libraries on npm (also, these things seem to be changing rapidly in the typescript world).

Should I wrap all my code in a module? (I don't feel the need for modules in the library itself, it's currently only at around 1000 LOC). Should I create an index.ts file? I think I should ship .js, .js.map and .d.ts files but no .ts files for the npm package?

How do i call functions between files in the library without exporting them to users of the library? I currently don't use typescript modules. I tried using typedoc and it lists also symbols that I exported from individual files for the purpose of using in another file of the library. But I don't want these to be visible to the users of library.

Is there a library I could use as an example? I looked at typescript-collections, they don't use any module and have an index.ts. I think they export all their shared functions, so that doesn't help me with that issue.

2

There are 2 answers

7
Qiaosen Huang On BEST ANSWER

What I did was having the index.ts export all files.

like

export { default as Actions } from './Actions'

then, in you package.json, add property "typings": "dist/index.d.ts" (change the location accordingly)

and yes, export definitions and source-maps. others can use source-map-support package to debug it easily later on.

I prefer pre-build them into .js when publishing.

0
Alex JM On

I'd suggest you to take a look at TypeScript Library Starter. It configures out of the box:

  • Package.json configuration
  • Universal module bundles
  • Source Maps
  • Typings (.d.ts)
  • Docs using TypeDoc
  • Tests and coverage