vesii
vesii

Reputation: 3128

How to use the 'main' parameter in package.json in TypeScript project?

I'm trying to create a new NPM package with TypeScript & Node. The package just allows the users to use a data structure I built. I'm trying to figure what is the right entry point (the value of main) should be in my package.json file. As I understand, I'm going to compile the TypeScript files (which are located in src/) into dist/ directory and those files will be actually hosted in NPM.

So should I have index.ts or index.js? If it's index.ts I'll need to compile it as well right? If it's index.js, then I need to point it to dist/? I'm a bit confused about what is the "right"/"convention" way to do it. At the end, I just want users to be able to load the classes I export in my src/mylist.ts file (which being compiled to dist/mylist.js). Do I even need it?

I was reading the How to use the 'main' parameter in package.json? topic, but it only refers to JS project. How should it be in TypeScript?

Upvotes: 4

Views: 2872

Answers (1)

Dimava
Dimava

Reputation: 10899

As typescript is generally compiled before distribution, you should compile it and use dist/index.js

You will need this do define what import {} from 'my-module' means (i.e. import {} from 'my-module' is threated same as import {} from 'my-module/dist/index.js')

If you don't publish your module and are not using it as a dependency of some other your module you don't need this at all, just make a scripts: { start: "tsx src/index.ts" } or whatever

Upvotes: 6

Related Questions